@@ -114,6 +114,35 @@ parser.parse_args(["--numbers", "1", "2", "3"])
114114assert parser.numbers == frozenset ([1 , 2 , 3 ])
115115```
116116
117+ ## Boolean arguments
118+
119+ Boolean arguments can be specified using like this:
120+
121+ <!-- - name: test_bools --->
122+ ``` python
123+ import argclass
124+
125+
126+ class ArgumentParser (argclass .Parser ):
127+ # Complete form you have to set default and action
128+ stored_true: bool = argclass.Argument(
129+ action = argclass.Actions.STORE_TRUE ,
130+ default = False
131+ )
132+ # Short form with default value
133+ # This is the alias for: argclass.Argument(action=argclass.Actions.STORE_TRUE, default=False)
134+ stored_true_short: bool = False
135+ # This is the alias for: argclass.Argument(action=argclass.Actions.STORE_FALSE, default=True)
136+ stored_false: bool = True
137+
138+
139+ parser = ArgumentParser(auto_env_var_prefix = ' APP_' )
140+ arguments = parser.parse_args([" --stored-true-short" ])
141+ assert arguments.stored_true is False
142+ assert arguments.stored_true_short is True
143+ assert arguments.stored_false is True
144+ ```
145+
117146## Configuration Files
118147
119148Parser objects can get default values from environment variables or from specified configuration files.
@@ -538,3 +567,72 @@ parser.parse_args(["--gizmo=off", "--optional=10"])
538567assert parser.gizmo is False
539568assert parser.optional == 10
540569```
570+
571+ # 3rd Party Libraries integration
572+
573+ ` argclass ` is able to integrate with some 3rd party libraries to provide additional features.
574+
575+ ## ` Rich ` and ` rich_argparse ` integration examples
576+
577+ ` rich_argparse ` is a library that provides an ability to use ` rich ` for formatting help messages in ` argparse ` .
578+ So this library can be used with ` argclass ` to provide a rich help output.
579+
580+ ``` python
581+ from argparse import Action
582+
583+ import argclass
584+ from rich.console import ConsoleRenderable, Group
585+ from rich.markdown import Markdown
586+ from rich.panel import Panel
587+ from rich.syntax import Syntax
588+ from rich.text import Text
589+ from rich_argparse import RawTextRichHelpFormatter
590+
591+
592+ class HelpFormatter (RawTextRichHelpFormatter ):
593+ def _rich_expand_help (self , action : Action) -> Text:
594+ try :
595+ if " %" in str (action.default):
596+ action.default = " "
597+ if " %" in str (action.help):
598+ action.help = " "
599+ return super ()._rich_expand_help(action)
600+ except ValueError :
601+ return Text(" FAILED" )
602+
603+
604+ class RichParser (argclass .Parser ):
605+ def __init__ (self , * args , ** kwargs ) -> None :
606+ help = kwargs.pop(" help" , None )
607+ description = kwargs.pop(" description" , help ) or " "
608+
609+ if isinstance (description, ConsoleRenderable):
610+ kwargs[" description" ] = description
611+ else :
612+ kwargs[" description" ] = Markdown(description)
613+
614+ if help is not None :
615+ kwargs[" help" ] = help
616+
617+ kwargs[" formatter_class" ] = HelpFormatter
618+ super ().__init__ (* args, ** kwargs)
619+
620+
621+ class Parser (RichParser ):
622+ log_level = argclass.LogLevel
623+
624+
625+ if __name__ == " __main__" :
626+ parser = Parser(
627+ formatter_class = RawTextRichHelpFormatter,
628+ description = Group(
629+ Text(" This code produces this help:\n\n " ),
630+ Panel(Syntax(open (__file__ ).read().strip(), " python" )),
631+ ),
632+ )
633+ parser.parse_args()
634+ parser.sanitize_env()
635+ exit (parser())
636+ ```
637+
638+ [ ![ Help Output] ( https://raw.githubusercontent.com/mosquito/argclass/master/docs/images/rich_help_output.png )] ( https://raw.githubusercontent.com/mosquito/argclass/master/.github/rich_example.png )
0 commit comments