Fork me on GitHub

      opt.py

            This is ttv1 code (Timm tools, version 1).

#

 
 
   home | discuss | report bug



#

Easier Command-Line Options

Documentation for this code is available
[on-line](http://tttv1.net/GLOBALS.py).
#

      Synopsis

            This is ttv1 code (Timm tools, version 1).

Install via `wget -O options.py http://ttvi1.net/GLOBALS.py`. Then use as follows:

     from opt import *
#
     our = options( 
       "1 line header",
       "Preamble text (multi-line).",
       "End text (multi-line).",
       group1 = [
         "1 line group description",
          ,h("1 line help",keyword1 = num")       # "--keyword1 X" expects any float
          ,h("1 line help",keyword2 = False")     # "--keyword2" will set keyword=True
          ,h("1 line help",keyword3 = str)        # "--keyword3 X" expects any string
          ,h("1 line help",keyword4 = [x,y,z..]") # "--keyword4 X" expects one of x,y,z...
#

where the type of "X" is set from "x"

       ],
       group2 = [
         ...
       ])

After that, data on the above groups is stored in `our`; e.g. `our.group1.keyword1`.
#

      Description

            This is ttv1 code (Timm tools, version 1).

The above call defines a variable `our` with fields (e.g.) 

     our.group1.keyword1

etc. Also, on the command line, 

     python codeThatUsesOptions.py--keyword1 X --keyword2 Y
     
will override the defaults shown below. Further, 

     python --help
     
will print help text, divided into the groups.
#

      Installation

            This is ttv1 code (Timm tools, version 1).

    wget -O options.py http://tiny.cc/ttv1options
#

      How it Works

            This is ttv1 code (Timm tools, version 1).

The standard way to process options in Python is the `argparse`
library which can be verbose, to say the least.  So this code is
an expansion function that takes a simple declarative syntax of the
optopms, then expands it into the argparse commands.

There's two functions that handle this process:

- `our=options(about,header,footer, groups)` writes a dictionary of options to `our`,
  while first checking if any command-line options overrides the defaults.
- `h(help,key=default)` is for one item. It exapnds into a whole
  dictionary of options for argparse.

Here are some examples of this expansions. For example,

       h("disable all tests", brave = False)

expands into

      dict(help    = "disable all test",
           key     = "--brave",
           default = False,
           action  = "store_true")

and

      h("split redos", splitRedo  = 20)

exapnds into

     dict(metavar = "S",
          key     = "--when",
          type    = str,
          choices = ["mon","tues","wed"],
          help    = "day of week",
          default = "mon"
         )

and 

      h("day of week", when=["mon","tues","wed"])

expands into

     dict(metavar = "S",
          key     = "--when",
          type    = str,
          choices = ["mon","tues","wed"],
          help    = "day of week",
          default = "mon"
         )

In the above `"mon"` was used as the default since it was
the first item in the when list.
#

import sys,argparse

def h(help,d): key,val = next(iter(d.items())) default = val[0] if isinstance(val,list) else val # step0: remember defaults out = dict(default=default) add = lambda d : out.update(d) # convenience function for adding args # step1: Set type and meta var if val is not False: if isinstance(default,int ): add(metavar= "I", type= int) elif isinstance(default,float): add(metavar= "F", type= float) else : add(metavar= "S", type= str) # step2: add help and type-specific misc flags if val is False : add(help=help, action="store_true") elif isinstance(val,list): add(help=help, choices=val) else: add(help=help + ("; e.g. %s" % str(val))) # -------------- return key, out

def options(prog, before, after, d): class o: def __init__(i, d) : i.dict.update(d) def repr(i) : return i.dict.repr() def getitem(i, k) : return i.dict[k] def setitem(i,k,v) : i.dict[k] = v # ------------------------------- parser = argparse.ArgumentParser( prog = prog, description = before, epilog = after, formatter_class=argparse.RawTextHelpFormatter) inside,out = {}, o() for context in sorted(d.keys()): out[context] = o() description = d[context][0] group = parser.add_argument_group(context, description) for key,rest in d[context][1:]: group.add_argument("--" + key,**rest) assert key not in inside, 'keys cannot repeat' inside[key] = context parsed = vars(parser.parse_args()) for key,val in parsed.items(): out[inside[key]][key]= val return out

____


<img align=right 
src="https://raw.githubusercontent.com/timm/timm.github.io/master/timm.png"
width=170>
#

      Copyleft

            This is ttv1 code (Timm tools, version 1).

Copyright &copy; 2016,2017 Tim Menzies <tim@menzies.us>, MIT license v2.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Share and enjoy.
#