Package to create command line arguments for Python.
Project description
Argument 
Package to create command line arguments for Python.
How to use:
- Decorate the method you want to create an argument from with
@Argument
. - Decorate the method you want to call from command line with
@Command
. - Create a
ArgumentParser
(from this package not from argparse) - Call
parser.parse_args()
All keywords arguments to @Argument
are the same as in argparse.ArgumentParser.add_argument except for 'action' and 'nargs'.
'action' is a custom Action created to run the decorated method and 'nargs' is the number of parameters in the decorated method.
All keywords arguments to @Command
are the same as in argparse.ArgumentParser.add_argument plus 'aliases' and 'helpers'.
aliases
is a dict where the key is the parameter name and the value is an alias to use with one dash. Ex.: aliases={'docker': 'd'}
the method will be called when --docker
is passed ou -d
. helpers
is a dict where the key is the parameter name and the value is the
help info for that parameter. This info is showed when calls cli CLI_NAME METHOD_NAME --help
Exemples:
foo.py
from argument import Argument, ArgumentParser
@Argument
def bar():
print('hi')
parser = ArgumentParser()
parser.parse_args()
Result:
$ python foo.py --bar
hi
You can pass argument to the method
@Argument
def bar(baz=None):
print(baz)
$ python foo.py --bar Hello
Hello
To create commands
@Command
def command():
print('this is a command')
$ python foo.py command
this is a command
With arguments
@Command
def command_with_arg(arg, arg1=None):
print('this the command arg: %s, arg1: %s' % (arg, arg1))
$ python foo.py command_with_arg Hello
this the command arg: Hello, arg1: None
$ python foo.py command_with_arg Hello --arg1 World
this the command arg: Hello, arg1: World
Using a Class
class ClassCommand:
@staticmethod
@Argument
def argument_in_class():
print('argument_in_class')
@staticmethod
@Command
def command_in_class(arg=None):
print('command_in_class arg %s' % arg)
$ python foo.py classcommand --argument_in_class
argument_in_class
$ python foo.py classcommand command_in_class
command_in_class arg None
$ python foo.py classcommand command_in_class --arg=Ola
command_in_class arg Ola
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for polidoro-py-argument-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb2e0ed85c03f037481ea443b3485b342515d002086998d1f14586e869e3e42a |
|
MD5 | 6c525b27d1306d6a8332c308bdaf7455 |
|
BLAKE2b-256 | 022f8e89fc364d725b425789aa9b17dc7d2da79a729b4901a584a1c305e3164c |
Hashes for polidoro_py_argument-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2ecf5452cd3127b00525fc61837e054fe9569bb3623430496c2b702f10a76a8 |
|
MD5 | 24427267a0a7608fb595cfd727896752 |
|
BLAKE2b-256 | 6d187f5554a9d63ce0763c9a666f9c8db2f222b81d4bc93f57d5f46d3b1f3b5b |