Skip to main content

A declarative way to define `cmd2` argument parsers.

Project description

cmdargparse

pypi license

The package provides a declarative way to define cmd2 argument parsers.

Usage

The core functionality is driven by three components:

  • cmdargument for defining an argument parser
  • cmdfield for declaring the arguments
  • cmdcommand for attatching an argument parser to a command
class MyApplication(cmd2.Cmd):

    @cmdargument
    class _MyCommandArgs(cmdargument):
        my_argument: str = cmdfield.argument(...)
        my_option: str = cmdfield.option(...)
        my_flag: str = cmdfield.flag(...)

    @cmdcommand(_MyCommandArgs)
    def do_mycommand(self, args: _MyCommandArgs) -> None:
        args.pfields(self)

Example Application

Arguments (positional)

  • choices
    The restricted set of values allowed for the argument.
  • choicesmap
    Additional values allowed for the argument that each map to the specified value in the restricted set of values.
  • default
    The arguments default value if not specified.
    (Providing a default value makes the argument optional.)
  • type
    Explicitly specifies the type to be used by argparse.
    https://docs.python.org/3/library/argparse.html#type
  • help
    Brief description of the argument.
  • helpvar
    Reference name of the argument value in the help message.

Examples

arg: str | None = cmdfield.argument(
    ("alpha", "beta"),
    {
        "a": "alpha",
        "b": "beta",
    },
    default=None,
)

Options (non-positional, value-bound)

  • decl
    Specifies the primary option declaration.
  • altdecl
    Specifies the alternative option declaration.
  • choices
    The restricted set of values allowed for the option.
  • choicesmap
    Additional values allowed for the option that each map to the specified value in the restricted set of values.
  • default
    The options default value if not specified.
  • form
    Specifies the default form of the option declaration.
  • decls
    Explicitly specifies all option declarations.
  • more_decls
    Specifies additional option declarations.
  • type
    Explicitly specifies the type to be used by argparse.
    https://docs.python.org/3/library/argparse.html#type
  • required
    Marks the option as required.
  • help
    Brief description of the option.
  • helpvar
    Reference name of the option value in the help message.

Examples

xx: str | None = cmdfield.option()
yy: int | None = cmdfield.option([1, 2, 3])
zz: str = cmdfield.option(required=True)

Declarations

aa: str | None = cmdfield.option()                       # -aa  OR  --aa
bb: str | None = cmdfield.option("--cc")                 # -bb, --cc  OR  --cc
dd: str | None = cmdfield.option("-ee")                  # -ee  OR  --dd, -ee
ff: str | None = cmdfield.option("--gg", "-ii")          # --gg, -ii
jj: str | None = cmdfield.option(form="--")              # --jj
kk: str | None = cmdfield.option(form="-")               # -kk
mm: str | None = cmdfield.option(decls="--mm")           # --mm
nn: str | None = cmdfield.option(decls="-oo")            # -oo
pp: str | None = cmdfield.option(decls=("--rr", "-ss"))  # --rr, -ss
tt: str | None = cmdfield.option(more_decls="-uu")       # -tt, -uu

Flags (non-positional, non-value)

  • decl
    Specifies the primary flag declaration.
  • altdecl
    Specifies the alternative flag declaration.
  • invert
    Sets the value False instead of True if the flag is specified.
  • const
    The value to set if the flag is specified.
  • form
    Specifies the default form of the flag declaration.
  • decls
    Explicitly specifies all flag declarations.
  • more_decls
    Specifies additional flag declarations.
  • type
    Explicitly specifies the type to be used by argparse.
    https://docs.python.org/3/library/argparse.html#type
  • help
    Brief description of the flag.

Examples

vv: bool = cmdfield.flag()
ww: bool = cmdfield.flag(invert=True)
xx: float | None = cmdfield.flag(const=1.23)
yy: str | None = cmdfield.flag(const="text")
zz: Literal["ltr"] | None = cmdfield.flag(const="ltr")

Declarations

aa: bool = cmdfield.flag()                       # -aa  OR  --aa
bb: bool = cmdfield.flag("--cc")                 # -bb, --cc  OR  --cc
dd: bool = cmdfield.flag("-ee")                  # -ee  OR  --dd, -ee
ff: bool = cmdfield.flag("--gg", "-ii")          # --gg, -ii
jj: bool = cmdfield.flag(form="--")              # --jj
kk: bool = cmdfield.flag(form="-")               # -kk
mm: bool = cmdfield.flag(decls="--mm")           # --mm
nn: bool = cmdfield.flag(decls="-oo")            # -oo
pp: bool = cmdfield.flag(decls=("--rr", "-ss"))  # --rr, -ss
tt: bool = cmdfield.flag(more_decls="-uu")       # -tt, -uu

Q&A

Changing the default declaration form

The cmdargument decorator accepts a form argument to select the default declaration form for an argument parser, which can be either -name (default) or --name.

It is also possible to change the global default declaration form by using the cmdargument.default_form function.

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

cmdargparse-0.1.0.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cmdargparse-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file cmdargparse-0.1.0.tar.gz.

File metadata

  • Download URL: cmdargparse-0.1.0.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for cmdargparse-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6d7e8f3cd2b283487c5fe92cadaa941d230840f8ff8b175af5c24000e5206d27
MD5 6c3be6614c482510402431d09860b16e
BLAKE2b-256 c4689e931a18ac88ec3f247d12b17205d2f402d7da6487a2703ce1ee8177020e

See more details on using hashes here.

File details

Details for the file cmdargparse-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cmdargparse-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for cmdargparse-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8008e4023bb43cb5a6749c1be9460e826b083c9bbc5d5bb67a6a49106678dac3
MD5 9281a92b2d0a049b68527c10250272ab
BLAKE2b-256 aeb85498e6987ed16402f96e322de8ba4c29db8b8dfb4a75147adeda09cb8944

See more details on using hashes here.

Supported by

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