Coder Social home page Coder Social logo

argscript's Introduction

Argscript

argscript is a port of werkzeug.script to argparse. Argscript generates a an argparse command line parser from the function definitions of a script.

An action is a function in the same module starting with action_ which takes a number of arguments where every argument has a default. The type of the default value specifies the type of the argument. Arguments can then be passed by using --name=value from the shell. werkzeug.script

Basic Usage is the same as werkzeug.script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Docstring for test script"""

import argscript

# actions go here
def action_foo(arg1='test'):
    """doc for action foo
    
    arg1 -- first argument
    """
    print 'action_foo called with argument arg1: %s' % arg1 

def action_bar(msg='', times=1):
    """doc for action bar

    msg -- a message
    times -- number 
    """
    print 'action_lee called with arguments msg: %s, times: %s' %(msg, times)
    

if __name__ == '__main__':
    argscript.run()

Now you can run the script with the -h parameter to show a basic help. The docstring from the test.py script is reused in the help message.

$ ./test.py -h
usage: test.py [-h] {foo,bar} ...

Docstring for test script

positional arguments:
  {foo,bar}

optional arguments:
  -h, --help  show this help message and exit

To get the help message for a particular action call the script with the action name and the -h parameter:

$ ./test.py bar -h
usage: test.py bar [-h] [--msg MSG] [--times TIMES]

doc for action bar

optional arguments:
  -h, --help     show this help message and exit
  --msg MSG      a message
  --times TIMES  number

Again the function docstring is reuesed in the help message. If you specify a description for the arguments in the doc string (variable -- doc), they are reuesed too.

Now you can run the script with the action name and the parameters, and the action function will be called with them. If you obmit a parameter, the default value will be used to call the function.

$ ./test.py bar --msg='Hello World' --times=4

action_lee called with arguments msg: Hello World, times: 4

Installation

You can either install the development version from github:

git clone [email protected]:hoffmann/argscript.git
cd argscript
sudo python setup.py install

or install it from pypi.python.org:

pip install argscript

argscript's People

Contributors

hoffmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

imbolc

argscript's Issues

Allow for positional arguments to actions

Now only named arguments are allowed, but it would be nice to be able to issue eg.:

$ python manage.py test mytestmodule.TestCase.test_method

instead of:

$ python manage.py test --label=mytestmodule.TestCase.test_method

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.