Skip to main content

Package to create command line arguments for Python.

Project description

Argument Latest

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 @Commandare 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polidoro-py-argument-2.1.0.tar.gz (6.1 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page