Skip to main content

Build Status

Keywords to command line arguments

Convert your function with keyword arguments into command line arguments with one line of code.

Installation

pip install keyword2cmdline

Usage

Use the decorator command from the keyword2cmdline module to convert the function into command line arguments.

from keyword2cmdline import command

@command
def main(text="Hello world",
         language='en.US',
         exclamation_number=2,
         exclamation_sign="!",
         exclamation=True):
     ...


if __name__ == '__main__':
    main()

Check the example examples/hello_world.py.

$ python examples/hello_world.py -h
usage: hello_world.py [-h] [--exclamation_number EXCLAMATION_NUMBER]
                      [--exclamation_sign EXCLAMATION_SIGN]
                      [--exclamation EXCLAMATION] [--text TEXT]
                      [--language LANGUAGE]

optional arguments:
  -h, --help            show this help message and exit
  --exclamation_number EXCLAMATION_NUMBER
  --exclamation_sign EXCLAMATION_SIGN
  --exclamation EXCLAMATION
  --text TEXT
  --language LANGUAGE
$ python examples/hello_world.py
Hello world!!
>>> from examples.hello_world import main
>>> main.set_sys_args([])()
Hello world!!
$ python examples/hello_world.py --exclamation_number 10
Hello world!!!!!!!!!!
>>> from examples.hello_world import main
>>> main.set_sys_args("--exclamation_number 10".split())()
Hello world!!!!!!!!!!
$ python examples/hello_world.py --language hi.IN
नमस्ते दुनिया!!
>>> from examples.hello_world import main
>>> main.set_sys_args("--language hi.IN".split())()
नमस्ते दुनिया!!

For boolean variables, any string is True but empty string '' is False (you can customize that)

$ python examples/hello_world.py --exclamation ''
Hello world
>>> from examples.hello_world import main
>>> main.set_sys_args( ["--exclamation", ""])()
Hello world

To add help and more customization to the ArgumentParser.add_argument() see the example in examples/hello_world_customizations.py. Basically import a dummy class opts from keyword2cmdline and then pass all the arguments as if opts is a dict.

from keyword2cmdline import command, opts

@command
def main(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.

$ python examples/hello_world_customizations.py -h
usage: hello_world_customizations.py [-h]
                                     [--exclamation_sign EXCLAMATION_SIGN]
                                     [--exclamation EXCLAMATION] [--text TEXT]
                                     [--language LANGUAGE]
                                     [--exclamation_number EXCLAMATION_NUMBER]

Prints hello world with desired number of exclamation signs

optional arguments:
  -h, --help            show this help message and exit
  --exclamation_sign EXCLAMATION_SIGN
  --exclamation EXCLAMATION
                        Whether to use exclamation sign or not. Use empty
                        string '' for False.
  --text TEXT
  --language LANGUAGE
  --exclamation_number EXCLAMATION_NUMBER

Support 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)

from keyword2cmdline import click_like_command

@click_like_command
def main(text="Hello world",
         language='en.US',
         exclamation_number=2,
         exclamation_sign="!",
         exclamation=True):
    ...
$ python examples/hello_world.py --exclamation False
Hello world
>>> from examples.hello_world_click import main
>>> main.set_sys_args( ["--exclamation", "False"])()
Hello world

Support for argcomplete, lists, dicts and enums

(New feature in v1.3.0)

list and dict are parsed using json.loads. dict are merged with the default dict argument. enum.Enum are converted to strings and the corresponding string can be converted back to the enum object. A convenience class keyword2cmdline.EnumChoice is provided for using shortnames for enum object which might use long names to support argcomplete feature 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)]

Metadata

Release files for keyword2cmdline 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keyword2cmdline 2.0.1
File Size Uploaded
keyword2cmdline-2.0.1.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keyword2cmdline 2.0.1
File Interpreter ABI Platform
keyword2cmdline-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / keyword2cmdline-2.0.1.tar.gz

Download URL keyword2cmdline-2.0.1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b67b83d8ebe6e137dc21c6760b23eca2626a323f70c5c61b9b43262a9691aae6
BLAKE2b-256 checksum
How to use checksums
04cc15a28eae36acd98bcaa259ef4b4e2169e67fa3bbf0b1266b20dbe8e836ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/20.7.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

Release files / keyword2cmdline-2.0.1-py3-none-any.whl

Download URL keyword2cmdline-2.0.1-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
646ab87d69fc328b0ef78276123a4a5a51c562a5777dc5d6d6eb5d321f0779b8
BLAKE2b-256 checksum
How to use checksums
3447032839b3cc77020940f865ca11a4852724b478c1b2484b6037940733b057
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/20.7.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

0.2.1

1 release file

0.2.0

3 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page