3 import argparse as _argparse
4 import logging as _logging
6 from pycomedi import LOG as _LOG
7 import pycomedi.constant as _constant
10 class _SetFrequencyAction (_argparse.Action):
11 def __call__(self, parser, namespace, values, option_string=None):
12 setattr(namespace, self.dest, values)
13 setattr(namespace, 'period', 1.0/values)
16 class _IncrementVerbosityAction (_argparse.Action):
17 def __call__(self, parser, namespace, values, option_string=None):
19 level -= 10 # e.g. logging.INFO -> logging.DEBUG
20 if level >= _logging.DEBUG:
24 # name -> (args, kwargs)
28 {'default':'/dev/comedi0',
29 'help':'path to comedi device file'}),
31 ['-s', '--subdevice'],
33 'help':'subdevice for analog input/output'}),
38 'help':'channel for analog input/output'}),
41 {'type':lambda x: [int(i) for i in x.split(',')],
43 'help':'comma-separated channels for analog input/output'}),
45 ['-a', '--analog-reference'],
47 'default':_constant.AREF.ground,
48 'type':lambda x: _constant.AREF.index_by_name(x),
49 'choices':_constant.AREF,
50 'help':'reference for analog input/output'}),
55 'help':'range for analog input/output'}),
57 ['-N', '--num-scans'],
60 'help':'number of input/output scans'}),
62 ['-F', '--frequency'],
64 'action':_SetFrequencyAction,
65 'help':'scan frequency in hertz'}),
69 'action':'store_const',
71 'help':'convert input to physical values before printing'}),
75 'action':'store_const',
77 'help':('use a memory-mapped reader/writer rather than '
78 'reading/writing the input/output subdevice directly')}),
81 {'action':_IncrementVerbosityAction}),
84 def parse_args(description, argnames, args=None):
86 >>> args = parse_args(
87 ... description='Parse a bunch of arguments',
88 ... argnames=['frequency'], args=['--frequency', '2'])
92 parser = _argparse.ArgumentParser(description=description)
93 for argument in argnames:
94 args_,kwargs = ARGUMENTS[argument]
95 parser.add_argument(*args_, **kwargs)
96 args = parser.parse_args(args=args)
97 if 'frequency' in argnames and not hasattr(args, 'period'):