Back to index

python3.2  3.2.2
Classes | Functions | Variables
CommandLine Namespace Reference

Classes

class  Option
 Option classes. More...
class  ArgumentOption
class  SwitchOption
class  Application

Functions

def _getopt_flags
 Helpers.
def invisible_input
def fileopen
def option_dict
def srange
def abspath
def _test

Variables

string __copyright__
string __version__ = '1.2'
 getpasswd = invisible_input
tuple _integerRE = re.compile('\s*(-?\d+)\s*$')
tuple _integerRangeRE = re.compile('\s*(-?\d+)\s*-\s*(-?\d+)\s*$')
 CommandLine = Application

Detailed Description

CommandLine - Get and parse command line options

    NOTE: This still is very much work in progress !!!

    Different version are likely to be incompatible.

    TODO:

    * Incorporate the changes made by (see Inbox)
    * Add number range option using srange()

Function Documentation

def CommandLine._getopt_flags (   options) [private]

Helpers.

Convert the option list to a getopt flag string and long opt
    list

Definition at line 29 of file CommandLine.py.

00029 
00030 def _getopt_flags(options):
00031 
00032     """ Convert the option list to a getopt flag string and long opt
00033         list
00034 
00035     """
00036     s = []
00037     l = []
00038     for o in options:
00039         if o.prefix == '-':
00040             # short option
00041             s.append(o.name)
00042             if o.takes_argument:
00043                 s.append(':')
00044         else:
00045             # long option
00046             if o.takes_argument:
00047                 l.append(o.name+'=')
00048             else:
00049                 l.append(o.name)
00050     return ''.join(s), l

Here is the caller graph for this function:

def CommandLine._test ( ) [private]

Definition at line 625 of file CommandLine.py.

00625 
00626 def _test():
00627 
00628     class MyApplication(Application):
00629         header = 'Test Application'
00630         version = __version__
00631         options = [Option('-v','verbose')]
00632 
00633         def handle_v(self,arg):
00634             print('VERBOSE, Yeah !')
00635 
00636     cmd = MyApplication()
00637     if not cmd.values['-h']:
00638         cmd.help()
00639     print('files:',cmd.files)
00640     print('Bye...')

def CommandLine.abspath (   path,
  expandvars = os.path.expandvars,
  expanduser = os.path.expanduser,
  join = os.path.join,
  getcwd = os.getcwd 
)
Return the corresponding absolute path for path.

    path is expanded in the usual shell ways before
    joining it with the current working directory.

Definition at line 135 of file CommandLine.py.

00135 
00136             join=os.path.join,getcwd=os.getcwd):
00137 
00138     """ Return the corresponding absolute path for path.
00139 
00140         path is expanded in the usual shell ways before
00141         joining it with the current working directory.
00142 
00143     """
00144     try:
00145         path = expandvars(path)
00146     except AttributeError:
00147         pass
00148     try:
00149         path = expanduser(path)
00150     except AttributeError:
00151         pass
00152     return join(getcwd(), path)

Here is the call graph for this function:

Here is the caller graph for this function:

def CommandLine.fileopen (   name,
  mode = 'wb',
  encoding = None 
)
Open a file using mode.

    Default mode is 'wb' meaning to open the file for writing in
    binary mode. If encoding is given, I/O to and from the file is
    transparently encoded using the given encoding.

    Files opened for writing are chmod()ed to 0600.

Definition at line 63 of file CommandLine.py.

00063 
00064 def fileopen(name, mode='wb', encoding=None):
00065 
00066     """ Open a file using mode.
00067 
00068         Default mode is 'wb' meaning to open the file for writing in
00069         binary mode. If encoding is given, I/O to and from the file is
00070         transparently encoded using the given encoding.
00071 
00072         Files opened for writing are chmod()ed to 0600.
00073 
00074     """
00075     if name == 'stdout':
00076         return sys.stdout
00077     elif name == 'stderr':
00078         return sys.stderr
00079     elif name == 'stdin':
00080         return sys.stdin
00081     else:
00082         if encoding is not None:
00083             import codecs
00084             f = codecs.open(name, mode, encoding)
00085         else:
00086             f = open(name, mode)
00087         if 'w' in mode:
00088             os.chmod(name, 0o600)
00089         return f

Here is the call graph for this function:

def CommandLine.invisible_input (   prompt = '>>> ')
Get raw input from a terminal without echoing the characters to
    the terminal, e.g. for password queries.

Definition at line 51 of file CommandLine.py.

00051 
00052 def invisible_input(prompt='>>> '):
00053 
00054     """ Get raw input from a terminal without echoing the characters to
00055         the terminal, e.g. for password queries.
00056 
00057     """
00058     import getpass
00059     entry = getpass.getpass(prompt)
00060     if entry is None:
00061         raise KeyboardInterrupt
00062     return entry

def CommandLine.option_dict (   options)
Return a dictionary mapping option names to Option instances.

Definition at line 90 of file CommandLine.py.

00090 
00091 def option_dict(options):
00092 
00093     """ Return a dictionary mapping option names to Option instances.
00094     """
00095     d = {}
00096     for option in options:
00097         d[option.name] = option
00098     return d
00099 
# Alias
def CommandLine.srange (   s,
  integer = _integerRE,
  integerRange = _integerRangeRE 
)
Converts a textual representation of integer numbers and ranges
    to a Python list.

    Supported formats: 2,3,4,2-10,-1 - -3, 5 - -2

    Values are appended to the created list in the order specified
    in the string.

Definition at line 108 of file CommandLine.py.

00108 
00109            integerRange=_integerRangeRE):
00110 
00111     """ Converts a textual representation of integer numbers and ranges
00112         to a Python list.
00113 
00114         Supported formats: 2,3,4,2-10,-1 - -3, 5 - -2
00115 
00116         Values are appended to the created list in the order specified
00117         in the string.
00118 
00119     """
00120     l = []
00121     append = l.append
00122     for entry in s.split(','):
00123         m = integer.match(entry)
00124         if m:
00125             append(int(m.groups()[0]))
00126             continue
00127         m = integerRange.match(entry)
00128         if m:
00129             start,end = map(int,m.groups())
00130             l[len(l):] = range(start,end+1)
00131     return l

Here is the call graph for this function:


Variable Documentation

Initial value:
00001 """\
00002 Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com)
00003 Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com)
00004 See the documentation for further information on copyrights,
00005 or contact the author. All Rights Reserved.
00006 """

Definition at line 16 of file CommandLine.py.

Definition at line 23 of file CommandLine.py.

tuple CommandLine._integerRangeRE = re.compile('\s*(-?\d+)\s*-\s*(-?\d+)\s*$')

Definition at line 103 of file CommandLine.py.

tuple CommandLine._integerRE = re.compile('\s*(-?\d+)\s*$')

Definition at line 102 of file CommandLine.py.

Definition at line 623 of file CommandLine.py.

Definition at line 100 of file CommandLine.py.