Create command line applications like Django management commands.
Read the full documentation at http://commis.readthedocs.org/.
To install the Commis library, the simplest thing to do is use pip
as follows:
$ pip install commis
Alternatively, you can download the latest (development) version or clone directly from Github and install it using the setup.py
script:
$ git clone https://github.com/bbengfort/commis.git
$ cd commis
$ python setup.py install
There is a bit more detail in the full documentation, but basically the way you create commands is to subclass the Command
class, define its name
, help
, and args
attributes, then implement a handle
method. This more or less looks as follows:
class GreetingCommand(Command):
name = "greeting"
help = "an example command, delivers a greeting"
args = {
# Language selection option
('-l', '--lang'): {
'type': str,
'default': 'english',
'metavar': 'LANG',
'choices': ['english', 'french', 'spanish'],
'help': 'the language of the greeting',
},
# List of one or more name arguments
'name': {
'nargs': "+",
'type': str,
'help': 'the name to greet you by'
}
}
def handle(self, args):
"""
Greet each person by name.
"""
salutation = {
'french': 'Bonjour',
'spanish': 'Hola',
'english': 'Hello',
}[args.lang.lower()]
output = []
for name in args.name:
output.append("{} {}!".format(salutation, name))
return "\n".join(output)
The args
is a Python dictionary of keyword arguments to be passed to argparse.add_argument
and uses the same syntax. See the add_argument method for more details.
The handle
method should accept args as its only argument, and should return a string to be printed to the command line.
Once you have your commands, you need to create and define a console utility to execute them. Simply subclass ConsoleProgram
with your definition as follows:
DESCRIPTION = "Inspects the mimetype distribution of a directory."
EPILOG = "Created for scientific purposes and not diagnostic ones."
COMMANDS = [
GreetingCommand,
]
class ExampleUtility(ConsoleProgram):
description = DESCRIPTION
epilog = EPILOG
VERSION = "1.0"
version = VERSION
@classmethod
def load(klass, commands=COMMANDS):
utility = klass()
for command in commands:
utility.register(command)
return utility
The ConsoleProgram
has a register
method which allows you to register various commands. You can do this at run time, creating dynamic utilities with different commands, or you can explore a directory and load all commands from it. Here I just simply add a simple load
static method to load the commands and instantiate the utility. Using the utility is then as simple as creating a script as follows:
from example import ExampleUtility
if __name__ == '__main__':
utility = ExampleUtility.load()
utility.execute()
For more on writing console utilities and programs, check out the documentation at http://commis.readthedocs.org/.
I'm not sure why this doesn't exist yet, but I needed a library for creating command line utilities that wrapped the argparse
library. Most of the ones I found used a decorator based syntax; but I really do like the Django management commands style of creating applications. Therefore, this library serves to give you the ability to do so!
Commis is open source, and I would be happy to have you contribute! You can contribute in the following ways:
- Create a pull request in Github: https://github.com/bbengfort/commis
- Add issues or bugs on the bug tracker: https://github.com/bbengfort/commis/issues
- Checkout the current dev board on waffle.io: https://waffle.io/bbengfort/commis
Note that labels in the Github issues are defined in the blog post: How we use labels on GitHub Issues at Mediocre Laboratories.
If you've contributed a fair amount, I'll give you direct access to the repository, which is set up in a typical production/release/development cycle as described in A Successful Git Branching Model. A typical workflow is as follows:
-
Select a card from the dev board - preferably one that is "ready" then move it to "in-progress".
-
Create a branch off of develop called "feature-[feature name]", work and commit into that branch.
~$ git checkout -b feature-myfeature develop
-
Once you are done working (and everything is tested) merge your feature into develop.
~$ git checkout develop ~$ git merge --no-ff feature-myfeature ~$ git branch -d feature-myfeature ~$ git push origin develop
-
Repeat. Releases will be routinely pushed into master via release branches, then deployed to the server.
Note that pull requests will be reviewed when the Travis-CI tests pass, so including tests with your pull request is ideal!
You can contact me on Twitter if needed: @bbengfort
com · mis
/ˈkämē,kô-/
noun a junior chef.
Origin
[Latin] committere: 1930s: from French, 'deputy, clerk', past participle of commettre 'entrust', from Latin.<br >
A commis is a basic chef in larger kitchens who works under a chef de partie to learn the station's or range's responsibilities and operation.[3] This may be a chef who has recently completed formal culinary training or is still undergoing training. — Wikipedia
This package is closely related to the Confire configuration tool, hence the name in the same vein — French cooking words. In this case, a commis is someone to whom orders are given (commands) and seemed an appropriate term for a package whose function is accept and execute commands.
The photo used in this README, “Pâte de fruit (gominolas) de laranxa sanguina” by Receitasparatodososdias is used under a CC BY-NC-ND 2.0 creative commons license.
The release versions that are sent to the Python package index are also tagged in Github. You can see the tags through the Github web application and download the tarball of the version you'd like. Additionally PyPI will host the various releases of commis.
The versioning uses a three part version system, "a.b.c" - "a" represents a major release that may not be backwards compatible. "b" is incremented on minor releases that may contain extra features, but are backwards compatible. "c" releases are bug fixes or other micro changes that developers should feel free to immediately update to.
This milestone will add Python 3 compatibility as well as new features like Confire integration and progress bars. Work for this milestone hasn't been set yet, so please feel to contribute issues and ideas!
- tag: v0.2
- deployment: January 25, 2016
- commit: e2529e6
Solidified the Commis library by improving the test suite and the documentation. I've also included a couple of modules that were big helps in the past: a color library that wraps colorama, and a LabelCommand. This is really the official "first" version that I feel is production ready.
- tag: v0.1
- deployment: January 20, 2016
- commit: 35ca6df
This is the initial release, which will simply bring over all the code and features that have previously been implemented in other locations. This release has also been published to PyPI and has some initial documentation.