Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Python library Console/Router creates opportunity to use convenient routing configuration for processing console application with sophisticated system of run params (processing different actions for diff params)
tree: 5b065b3388

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
PyConsole
README

README

INTRODUCTION
=====================

Python library Console/Router creates opportunity to use convenient routing configuration
for processing console application with sophisticated system of run arguments
(processing different actions for different arguments).

For example, you use default configuration and added one route "scheme dump". In this case,
when user launch your script with next arguments:

%> my_script.py scheme dump -file log.txt -lines 20

Router will execute function scheme_dump(file="log.txt", lines="20").
You can change this behavior by set up other function, adding help information,
set up module to get function from or creating rules for params validation.

GENERAL USE CASES
=====================

Using this library you can:
- match list of pass to console application params to function from global namespace or from specified module
- add validators for named attributes (for ex. %>my_script.py save -file my_file.txt)
- set helper string for each list of arguments which can be used by passing --help key in the end of arguments list
- create group of sub-argument for given argument (for ex., after file argument, system will wait for on
  of the next: 'open', 'save', 'edit'), in this case help string for file argument will be generated automaticly

For get code samples on described use cases, please, see __main__ block in source code.

ADVANCED USAGE
=====================

Also you can:
- set new validation functions (VALIDATORS) and errors for invalid arguments (ERRORS) for easy reusing in future
- easy change help strings rendering by extending _render_help() and _render_custom_help() methods
- use this library for initial application routing without arguments passed by user, 
  cause you can call execute with any necessary list of commands

REFERENCE GUIDE
=====================

Available methods:

Router.__init__(routes)

    You can specify all routes by passing list as object constructor argument.

Router.add_route([list of arguments], function, module, params, help)

    Add new routing rules by given list of argument for matching this route and additional information
    about what system has to do, when users arguments will match this route:
        function - what function to call (arguments joined by underscore as default)
        module   - from what module (global namespace as default)
        params   - validation rules for named argument values (empty dictionary as default)
        help     - helper string which will be shown if user pass --help key
                   after all arguments (empty string as default)

Router.add_group

    Add group of routers.

Router.set_routes([list of new routes])

    Overwrite all existen routes by list of given.

Router.execute([arguments])

    Routing processing. In most cases you have to call this function like this:
    router.execute(sys.argv[1:])

ADDITIONAL NOTES
=====================

For fixing some bug or adding/changing library features, please, open Issue on GitHub project page:
https://github.com/kachayev/console-router
or write me email on <kachayev@gmail.com>. I will try to answer you issue in nearest time.

LICENSE
=====================

Copyright (c) 2010, Alexey S. Kachayev <kachayev@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of the author nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Something went wrong with that request. Please try again.