Next Steps¶
Once your current application is using cmd2
, you can start to expand the
functionality by levering other cmd2
features. The three ideas here will
get you started. Browse the rest of the Features to see
what else cmd2
can help you do.
Argument Parsing¶
For all but the simplest of commands, it’s probably easier to use argparse to parse user input.
cmd2
provides a @with_argparser()
decorator which associates an
ArgumentParser
object with one of your commands. Using this method will:
- Pass your command a Namespace containing the arguments instead of a string of text.
- Properly handle quoted string input from your users.
- Create a help message for you based on the
ArgumentParser
. - Give you a big headstart adding Completion to your application.
- Make it much easier to implement subcommands (i.e.
git
has a bunch of subcommands such asgit pull
,git diff
, etc).
There’s a lot more about Argument Processing if you want to dig in further.
Help¶
If you have lot of commands in your application, cmd2
can categorize those
commands using a one line decorator @with_category()
. When a user types
help
the available commands will be organized by the category you
specified.
If you were already using argparse
or decided to switch to it, you can
easily standardize all of your help messages to be generated by your argument
parsers and displayed by cmd2
. No more help messages that don’t match what
the code actually does.
Generating Output¶
If your program generates output by printing directly to sys.stdout
, you
should consider switching to poutput()
,
perror()
, and pfeedback()
. These
methods work with several of the built in Settings to
allow the user to view or suppress feedback (i.e. progress or status output).
They also properly handle ansi colored output according to user preference.
Speaking of colored output, you can use any color library you want, or use the
included cmd2.ansi.style()
function. These and other related topics are
covered in Generating Output.