Package: keyword2cmdline
Keywords to command line argumentsConvert your function with keyword arguments into command line arguments with one line of code.Installationpipinstallkeyword2cmdlineUsageUse the decoratorcommandfrom thekeyword2cmdlinemodule to convert the function into command line arguments.fromkeyword2cmdlineimportcommand@commanddefmain(text="Hello world",language='en.US',exclamation_number=2,exclamation_sign="!",exclamation=True):...if__name__=='__main__':main()Check the exampleexamples/hello_world.py.$pythonexamples/hello_world.py-h
usage:hello_world.py[-h][--exclamation_numberEXCLAMATION_NUMBER][--exclamation_signEXCLAMATION_SIGN][--exclamationEXCLAMATION][--textTEXT][--languageLANGUAGE]optionalarguments:-h,--helpshowthishelpmessageandexit--exclamation_numberEXCLAMATION_NUMBER--exclamation_signEXCLAMATION_SIGN--exclamationEXCLAMATION--textTEXT--languageLANGUAGE$pythonexamples/hello_world.py
Helloworld!!>>> from examples.hello_world import main
>>> main.set_sys_args([])()
Hello world!!$pythonexamples/hello_world.py--exclamation_number10Helloworld!!!!!!!!!!>>> from examples.hello_world import main
>>> main.set_sys_args("--exclamation_number 10".split())()
Hello world!!!!!!!!!!$pythonexamples/hello_world.py--languagehi.IN
नमस्तेदुनिया!!>>> from examples.hello_world import main
>>> main.set_sys_args("--language hi.IN".split())()
नमस्ते दुनिया!!For boolean variables, any string isTruebut empty string '' isFalse(you can customize that)$pythonexamples/hello_world.py--exclamation''Helloworld>>> from examples.hello_world import main
>>> main.set_sys_args( ["--exclamation", ""])()
Hello worldTo add help and more customization to theArgumentParser.add_argument()see the example inexamples/hello_world_customizations.py. Basically import a dummy classoptsfrom keyword2cmdline and then pass all the arguments as ifoptsis adict.fromkeyword2cmdlineimportcommand,opts@commanddefmain(text="Hello world",language='en.US',exclamation_number=2,exclamation_sign="!",exclamation=opts(default=True,type=bool,help="""Whether to use exclamation sign or not. Use empty string '' for False""")):...if__name__=='__main__':main()Asking for help will print the keyword help.$pythonexamples/hello_world_customizations.py-h
usage:hello_world_customizations.py[-h][--exclamation_signEXCLAMATION_SIGN][--exclamationEXCLAMATION][--textTEXT][--languageLANGUAGE][--exclamation_numberEXCLAMATION_NUMBER]Printshelloworldwithdesirednumberofexclamationsigns

optionalarguments:-h,--helpshowthishelpmessageandexit--exclamation_signEXCLAMATION_SIGN--exclamationEXCLAMATIONWhethertouseexclamationsignornot.Useemptystring''forFalse.--textTEXT--languageLANGUAGE--exclamation_numberEXCLAMATION_NUMBERSupport for Variational **kwargs(New feature in v1.0)>>> from keyword2cmdline import command
>>> first = lambda xs: xs[0]
>>> @command
... def main(text="sum", **kw):
...     return dict(kw, text=text)
>>> _ = main.set_sys_args(sys_args = "--text sum --a 1 --b 2 --c 3".split())
>>> list(sorted(main().items(), key=first))
[('a', '1'), ('b', '2'), ('c', '3'), ('text', 'sum')]Support for click like boolean parser(New feature in v1.0)fromkeyword2cmdlineimportclick_like_command@click_like_commanddefmain(text="Hello world",language='en.US',exclamation_number=2,exclamation_sign="!",exclamation=True):...$pythonexamples/hello_world.py--exclamationFalse
Helloworld>>> from examples.hello_world_click import main
>>> main.set_sys_args( ["--exclamation", "False"])()
Hello worldSupport for argcomplete, lists, dicts and enums(New feature in v1.3.0)listanddictare parsed usingjson.loads.dictare merged with the
defaultdictargument.enum.Enumare converted to strings and the
corresponding string can be converted back to the enum object. A convenience
classkeyword2cmdline.EnumChoiceis provided for using shortnames forenumobject which might use long names to supportargcompletefeature consistently.>>> from keyword2cmdline import command, EnumChoice
>>> @command
... def main(text="Hello world",
...          language=EnumChoice('Lang', 'en_US hi_IN').en_US,
...          exclamation_props=dict(number=2),
...          exclamation=True):
...     return sorted(locals().items())
...
>>> main.set_sys_args([])()
[('exclamation', True), ('exclamation_props', {'number': 2}), ('language', <Lang.en_US: 1>), ('text', 'Hello world')]
>>> main.set_sys_args(["--exclamation", ""])()
[('exclamation', False), ('exclamation_props', {'number': 2}), ('language', <Lang.en_US: 1>), ('text', 'Hello world')]
>>> main.set_sys_args(["--language", "hi_IN"])()
[('exclamation', True), ('exclamation_props', {'number': 2}), ('language', <Lang.hi_IN: 2>), ('text', 'Hello world')]
>>> main.set_sys_args(["--exclamation_props", '{"number": 3}'])()
[('exclamation', True), ('exclamation_props', {'number': 3}), ('language', <Lang.en_US: 1>), ('text', 'Hello world')]

>>> from keyword2cmdline import click_like_command
>>> @click_like_command
... def main(text="Hello world",
...          language=EnumChoice('Lang', 'en_US hi_IN').en_US,
...          exclamation_props=dict(number=2),
...          exclamation=True):
...     return sorted(locals().items())
...
>>> main.set_sys_args(["--exclamation", "False"])()
[('exclamation', False), ('exclamation_props', {'number': 2}), ('language', <Lang.en_US: 1>), ('text', 'Hello world')]Support for recursive configs using command_config(New feature in v2.0.0)Recursive functions are handled by constructing partials of functions
that are marked with@command_config.>>> from keyword2cmdline import command, EnumChoice, command_config
>>> @command_config
... def exclamation(number=2,
...                 sign="!",
...                 use=True):
...     return sorted(locals().items())
>>> @command
... def main(text="Hello world",
...          language=EnumChoice('Lang', 'en_US hi_IN').en_US,
...          exclamation=exclamation):
...     return [("text", text), ("language", language)] + [
...            ("exclamation." + k, v)
...            for k, v in sorted(exclamation.keywords.items()) ]
>>> main.set_sys_args(["--exclamation.use", "True", "--exclamation.sign", "?"])()
[('text', 'Hello world'), ('language', <Lang.en_US: 1>), ('exclamation.number', 2), ('exclamation.sign', '?'), ('exclamation.use', True)]


A click like handling of booleans is available with `click_like_command_config`
and `click_like_command`.

``` python-console
>>> from keyword2cmdline import click_like_command, EnumChoice, click_like_command_config
>>> @click_like_command_config
... def exclamation(number=2,
...                 sign="!",
...                 use=True):
...     return sorted(locals().items())
>>> @click_like_command
... def main(text="Hello world",
...          language=EnumChoice('Lang', 'en_US hi_IN').en_US,
...          exclamation=exclamation):
...     return [("text", text), ("language", language)] + [
...            ("exclamation." + k, v)
...            for k, v in sorted(exclamation.keywords.items()) ]
>>> main.set_sys_args(["--exclamation.use", "False", "--exclamation.sign", "?"])()
[('text', 'Hello world'), ('language', <Lang.en_US: 1>), ('exclamation.number', 2), ('exclamation.sign', '?'), ('exclamation.use', False)]