Nanocli

Simple functions to extract parameters from configuration files and command line options
Download

Nanocli Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Rigel Di Scala
  • Publisher web site:
  • http://github.com/zedr/

Nanocli Tags


Nanocli Description

Simple functions to extract parameters from configuration files and command line options Nanocli is a library that can be used when writing small programs such as daemons and scripts, when you need to process command-line arguments (options) and read basic configuration values, with minimal setup and fuss.Usage-----Install nanocli as a Python Egg or simply copy 'nanocli.py' inside in yourproject.Now, all you have to do is import the module:: >>> from nanocli import nanocliand then run the functions:: >>> nanocli.get_exec() >>> nanocli.get_config(config=myconfig.cfg)or:: >>> nanocli.get_config()to read the .cfg file that has the same name of your script.get_exec() returns a tuple containing: 1. A list with all the switches; 2. A dictionary with short and long-options; 3. A list with all the operands.So a command like:: ./myscript.py -px --verbose --conf=my.cfg text.txtwill return:: (, {'verbose': True, 'conf': 'my.cfg'}, )Definitions-----------Nanocli prefers unamibiguous options: *Switch*: a single dash ('-') followed by a letter. Multiple letters meanmultiple switches. *Option*: a single dash, followed by a letter or a word and a mandatoryassignment operator ('='), that will assign an operand to that long-option. *Long-option*: a double dash ('--') followed by a letter or a word. Can befollowed by an assignment operator ('=') that will assign an operand to that long-option. *Operand*: any letter or a word that doesn't start with a dash.It intentionally does not follow the POSIX standard and the GNU extensions,though retainingsome compatibility.Examples----------- -p is a switch -bar is a group of 3 switches --foo is a long option --foo=bar is a long option with an argument foo is an operand foo bar is a group of 2 operands -p=foo is an option WITH an argument (deprecated) -p foo is a switch AND an operandFormat--------------A single dash always is followed by a single or a group of letters. Each lettermeans an option: -p -v -pv mean the same thing ('p','v')A double dash indicates a long-option: --option=argument means {'option': 'argument'] --with-flags=foo,bar means {'with-flags': --verbose means verbose=True and therefore {'verbose': True} --verbose= means nothing and will be ignoredNo initial dash indicates a command line operand: file1 file-2 means 'file1' AND 'file-2', therefore Anything that follows a double dash is considered a cli operand: foo -- -bar means Caveats-------- --verbose=False means {'verbose': 'False'} not {'verbose': False}; it returns a string, not a boolean.Backwards compatibility-----------------------A single dash followed by a single letter and an assignment is interpreted as along option: -p=80 means {'p': '80'} and will be parsed as an option. This is deprecated but accepted for backward compatibility reasons.However: -p80 -p 80 do NOT mean {'p': '80'}, but and (, .This is to simplify parsing and to make it unambiguous.Nanocli conventions-------------------Options should always have an equivalent long option: -v, --verbose -r, --recursiveCertain flags should be reserved for special keywords: -?, --help print usage() (if '-h' is not used then it's ok to alias it to '--help') -v, --verboseConfiguration parsing=====================Usage-----This is extremely simple. Just create a text file like:: """ # This is a comment key1 = 80 key2 = another value # This is another comment """get_config() will return a dictionary containing the following pairs:: { 'key1': '80', 'key2': 'another value' }That's it!Testing=======To test get_exec(), run 'clitest.py' with some command line options.For Unittests, run 'runtests.py'. Requirements: · Python


Nanocli Related Software