Object model

The decorators are useful for commands that are simple and follow basic CLI conventions. Sometimes you want to extend your CLI with custom features. This is where the object model comes in.

Each concept in a commandline program is represented by an object in the dryparse.objects module. Thus, each command is represented by an instance of Command, each option by an Option, etc.

The fundamentals

The simplest way to create a command is:

git = Command("git", desc="A version control software")

Adding options is super easy:

git.paginate = Option("-p", "--paginate")

Note

The git.paginate attribute didn’t exist before. We created it dynamically by assigning a value to it.

By default, options have a type of bool. This means that when the option is specified on the command line it will have a value of True, and False otherwise.

Let’s create an option of type int:

git.retries = Option("-r", "--retries", type=int)

This option will expect an argument to be specified via the command line. The argument is automatically converted to the type we specified (in this case int).

Note

All options except for bool and some special types take CLI arguments, and those arguments are automatically converted to the specified type.

Note that commands include a --help option by default, via a help attribute that is just like any other attribute. You can delete it if you don’t need it:

del git.help

Adding a subcommand

Adding a subcommand is just as easy as adding an option:

git.commit = Command("commit", desc="Record changes to the repository")
git.checkout = Command("checkout", desc="Switch branches or restore working tree files")

Defining positional arguments

git.add.args = Arguments([])

These use cases are simple, but Arguments has so much more to offer. Take a look at its API documentation.

Root command

CLI programs usually include a --version option in their root command. While you can add this option yourself, we provide RootCommand as a convenience:

git = RootCommand("git", version="0.1.0", desc="A version control software")