Skip to main content

The small framework for sub commands.

Project description

jumon is a small framework for sub commands.

The jumon is a small framework for implementing the sub command. You can be implemented sub commands, as the hierarchial structure of the sub modules in the packages. It finds importable sub module from the arguments passed, Runs main function in the importable sub module with arguments.

You can be displayed the hierarchial structure of the sub commands with tree command etc, because it be implementing sub commands as the hierarchial structure of the sub module. It will be easy to see where sub command has been implemented.

Please consider to use jumon, if you are trying to implement the sub commands.

Installing

Use setup.py in the source code root directory:

$ python setup.py install

by pip (from PyPI)

$ pip install jumon

How to use

This is a description of how to use the jumon. Let create a package called “testcmd”, and sub package called “testcmd.command”.

$ mkdir testcmd
$ touch testcmd/__init__.py
$ mkdir testcmd/command

Let implementing sub commands in “testcmd.command” subordinate.

First, create top level command.:

$ touch testcmd/command/__init__.py

testcmd/command/__init__.py:

import jumon

def main():
    jumon.entry(__name__)

Under this package is the search range of sub commands.

Next, create sub command.:

$ touch testcmd/command/aaa.py

testcmd/command/aaa.py:

def main(argv):
    print 'OK'
    return 0

This commamd is printed string “OK”, and end with exit code 0.

You can be implemented sub command of sub command.:

$ mkdir testcmd/command/bbb
$ touch testcmd/command/bbb/__init__.py
$ touch testcmd/command/bbb/ccc.py

testcmd/command/bbb/__init__.py:

def main(argv):
    print 'bbb'
    return 1

testcmd/command/bbb/ccc.py:

def main(argv):
    print 'ccc'
    return 2

I implemented sub commands, called “bbb” and “bbb ccc”. This commamd “bbb” is printed string “bbb”, and end with exit code 1. This commamd “bbb ccc” is printed string “ccc”, and end with exit code 2.

Next, install “testcmd”.:

$ cp testcmd $SITE_PACKAGES -R

Last, create command.:

$ touch test.py

test.py:

#! /usr/bin/env python
import testcmd.command

def main():
    testcmd.command.main()

if __name__ == '__main__':
    main()

Add permission:

$ chmod 755 test.py

Run, displayed usage.:

$ ./test.py
Usage: test.py [options]

test.py: error: Command Not Found:

Run sub command “aaa”.:

$ ./test.py aaa
OK
$ echo $?
0

Run sub command “bbb”.:

$ ./test.py bbb
bbb
$ echo $?
1

Run sub command “bbb ccc”.:

$ ./test.py bbb ccc
ccc
$ echo $?
2

Do you want to created command with setup.py?

setup.py:

#-*- coding: utf-8 -*-
from setuptools import setup, find_packages
setup(
    name='testcmd',
    version='1',
    license='BSD',
    author='TakesxiSximada',
    author_email='takesxi.sximada@gmail.com',
    packages=find_packages(),
    entry_points = """\
    [console_scripts]
    testcmd = testcmd.command:main
    """
)

This is important:

entry_points = """\
[console_scripts]
testcmd = testcmd.command:main
"""

Do you want to transparenting the undefined options?

You can use jumon.TransparentOptionParser() class.

>>> import jumon
>>> parser = jumon.TransparentOptionParser()
>>> opts, args = parser.parse_args(['-f', '-n', '1'])
>>> args
['-f', '-n', '1']

Requirements

  • Python 2.6 or laterr (not supprt 3.x)

License

Apache License 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jumon-1.0.1.tar.gz (5.6 kB view hashes)

Uploaded Source

Built Distribution

jumon-1.0.1-py2.7.egg (8.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page