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'}),
74 {'action':_IncrementVerbosityAction}),
77 def parse_args(description, argnames):
78 parser = _argparse.ArgumentParser(description=description)
79 for argument in ['filename', 'subdevice', 'channels', 'aref', 'range',
80 'num-scans', 'frequency', 'physical', 'verbose']:
81 args,kwargs = ARGUMENTS[argument]
82 parser.add_argument(*args, **kwargs)
83 return parser.parse_args()