Package: pycommand
Library / toolkit for creating command line programs with minimal effort.Pycommand is essentially a fancy wrapper around getopt that consists of
one simpleCommandBaseclass that you can inherit to create executable
commands for your (Python) programs with very simplistic and readable
code. It has support for subcommands and also nesting commands, so you
can create (multiple levels of) subcommands, with the ability to pass
the values of optional arguments of a command object to its subcommand
objects. Supported Python versions are 2.7 and 3.2 and later.Documentation:https://babab.github.io/pycommand/PyPI:https://pypi.python.org/pypi/pycommand/Github:https://github.com/babab/pycommandFeaturesParsing of optional and positional argumentsMinimalistic approach with a clean APICreate scripts in a matter of minutes using the code generatorAuto compiled usage messagesGraceful semi-automatic handling of exit status codesSubcommands can have subcommands that can have subcommands
(each with their own optional arguments)Pass values for –some-option from a parent command into child commands.Download and installIf you have pip installed, you can just do:#pipinstallpycommandScript generatorTo quickly start writing a command from a template (much like the
examples below), use the script generator by running:$python-mpycommandinitThis will ask you for an executable name, class name and template type
and it will save it to an executable python script, ready to be used as
a command line program.You can have a very basic command line program that handles-v,--versionand-h,--helparguments set up in less than a minute.ExampleFor full documentation and examples, visithttp://pythonhosted.org/pycommand/Here is an undocumented code example of getting automated usage text
generation and parsing of optional arguments. If we name the script
for which you can see the code belowbasic-exampleand execute it,
the following will be the output for runningbasic-example-horbasic-example--help:usage: basic-example [options]

An example of a basic CLI program

Options:
-h, --help                        show this help information
-f <filename>, --file=<filename>  use specified file
--version                         show version informationAnd here is the code:#!/usr/bin/env pythonimportpycommandimportsysclassBasicExampleCommand(pycommand.CommandBase):'''An example of a basic CLI program'''usagestr='usage: basic-example [options]'description=__doc__optionList=(('help',('h',False,'show this help information')),('file',('f','<filename>','use specified file')),('version',('',False,'show version information')),)defrun(self):ifself.flags.help:print(self.usage)return0elifself.flags.version:print('Python version '+sys.version.split()[0])return0elifself.flags.file:print('filename = '+self.flags.file)return0print('Program completed. Try adding "--help"')if__name__=='__main__':# Shortcut for reading from sys.argv[1:] and sys.exit(status)pycommand.run_and_exit(BasicExampleCommand)# The shortcut is equivalent to the following:# cmd = BasicExampleCommand(sys.argv[1:])# if cmd.error:#     print('error: {0}'.format(cmd.error))#     sys.exit(1)# else:#     sys.exit(cmd.run())Why was it created?When parsing command line program arguments, I sometimes work withargparse(a replacement foroptparse). I don’t really like the API
and the output it gives, which is the main reason I’ve always usedgetoptfor parsing arguments whenever possible.TheCommandBaseclass was originally written forDisPass,
which is a password manager/generator, as a means to easily define new
subcommands and have auto-generated usage messages. Because I want to
have this in other projects I’ve decided to put it in the cheeseshop in 2013.
It has since been refined for more generic usage and has proven to be
stable and workable throughout the years.Software licenseCopyright (c) 2013-2016, 2018 Benjamin Althues <benjamin@babab.nl>Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.Change Logpycommand adheres toSemantic Versioning.0.4.0 - 2018-03-27AddedFull templates can now (also) be auto generatedCI testing for Python 3.5 and 3.6ChangedNoteThepycommand initscript is removed and is included in the
pycommand package itself.To auto generate scripts from templates, from now on use:python -m pycommand initThe code is split up into several modules and pycommand is now
distributed as a package rather than a single module. The public
API does not change however, all relevant members (CommandBase,
run_and_exit) that are now placed in pycommand.pycommand are
exposed through __init__ and therefore are still available aspycommand.CommandBaseandpycommand.run_and_exit.Code generator is included in the package itself instead of
using an installed script (pycommand init)All templates are now embedded as wellRemovedPycommand init script (installed into /usr/local/bin)Templates directoryGNU info docs and manpage from distribution (they can still be generated)pycommand.3 (prev. installed into /share/man/man3)pycommand.info0.3.0 - 2015-06-04AddedShortcutrun_and_exit()for reading fromsys.argv[1:]and exiting
the interpreter viasys.exit(status)Package as wheel distribution to speed up installationsAddman pycommandability, i.e. install mandoc in/usr/share/man3/ChangedAdd support for getting flags by attribute likeself.flags.help.
The default approach for normal dicts likeself.flags['help']remains valid.0.2.0 - 2015-05-21AddedFull example of a command with subcommandsCreate quick templates via pycommand script (pycommand init)Unit tests and automatic testing via Travis-CIDocumentationman(.3) andinfo(.info) pagesChangedSpecification of subcommands can bedefined in CommandBase.commandas a shortcut.0.1.0 - 2013-08-08AddedInitial release