Python Clom


Clom is a Python Command Line Object Mapper. It’s intended to make generating commands and escaping arguments easier for command line interaction. It’s particularly useful when used with Fabric or subprocess.


Install with pip or easy_install.

pip install clom

Usage Examples


>>> from clom import clom

Build a command:

>>> clom.echo("Don't test me")
"echo 'Don'\\''t test me'"

Augment with arguments:

>>>'-a', '-t', l=True).with_args('~/')
"ls -a -t -l '~/'"
>>> clom.curl('', X='POST', data='message=hello')
'curl -X POST --data message=hello'

Use sub commands:

>>> clom.git.checkout('mybranch')
'git checkout mybranch'

Execute with ease:

>>> clom.seq(5).shell.all()
['1', '2', '3', '4', '5']


Iterate over results:

>>> for i in clom.seq(3).shell():
...     print(i)

Handle errors:

>>> clom.touch('/not/a/thing').shell()      
Traceback (most recent call last):
CommandError: Error while executing "touch /not/a/thing" (1):
touch: cannot touch ‘/not/a/thing’: No such file or directory

Group commands:

>>> from clom import AND, OR
>>> OR(clom.vagrant.up, clom.echo('Vagrant up failed'))
"( vagrant up || echo 'Vagrant up failed' )"
>>> OR(clom.vagrant.up, clom.echo('Vagrant up failed')).shell()
<CommandResult return_code=0, stdout=18 bytes, stderr=... bytes>
>>> print(OR(clom.false, clom.echo('Vagrant up failed')).shell())
Vagrant up failed

Re-use commands:

>>> echo = clom.echo
'echo one two'
['three four']
['foo bar']

Background tasks:

>>> clom.VBoxHeadless.with_opts(startvm="Windows Base").background()
"nohup VBoxHeadless --startvm 'Windows Base' &> /dev/null &"
>>> clom.VBoxHeadless.with_opts(startvm="Windows Base").background().shell()
<CommandResult return_code=0, stdout=0 bytes, stderr=0 bytes>

['"Windows Base" {949ec0af-92d0-4140-8a6c-36301ca6f695}']

Works great with fabric:

>>> from fabric.api import run, local           
>>> local(                              
[localhost] local: ls
clom            clom.egg-info   docs            nohup.out       tests

Can even create fab commands:

>>> clom.fab.test('doctest', 'unit').deploy('dev')
'fab test:doctest,unit deploy:dev'
>>> clom.fab.with_opts('-a', hosts='').deploy.with_args('dev','test')
'fab -a --hosts deploy:dev,test'

API Documentation