Skip to main content

def your CLIs

Project description

def your CLIs.

Use @subcommand on any function to add a subcommand to your cli. The function's arguments become the arguments to the subcommand. Uses argparse behind the scenes.

import defcli

@defcli.subcommand
def positional_integer(num: int):
    print(f"I like to add five: {num + 5}")

defcli.run()
$ python3 main.py positional_integer 7
I like to add five: 12

Or use named arguments.

import defcli

@defcli.subcommand
def named_string(*, name):
    print(f"Hello, {name}!")

defcli.run()
$ python3 main.py named_string --name Fred
Hello, Fred!

Named booleans are automatically converted to flags.

import defcli

@defcli.subcommand
def named_bool(has_something=False):
    if has_something:
        print("Got it!")
    else:
        print("We're empty-handed")

# Pass arguments to defcli.run instead of parsing sys.argv
defcli.run(["named_bool", "--has_something"])
$ python3 main.py
Got it!

Future

  • Allow @subcommand to take a parameter to allow the same program to define multiple CLI's.
  • Fix bugs (there are many...)

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

defcli-0.0.2.tar.gz (3.4 kB view hashes)

Uploaded Source

Built Distribution

defcli-0.0.2-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

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