1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
3 # This file is part of pycomedi.
5 # pycomedi is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 2 of the License, or (at your option) any later
10 # pycomedi is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # pycomedi. If not, see <http://www.gnu.org/licenses/>.
17 import argparse as _argparse
18 import logging as _logging
20 from pycomedi import LOG as _LOG
21 import pycomedi.constant as _constant
24 class _SetFrequencyAction (_argparse.Action):
25 def __call__(self, parser, namespace, values, option_string=None):
26 setattr(namespace, self.dest, values)
27 setattr(namespace, 'period', 1.0/values)
30 class _IncrementVerbosityAction (_argparse.Action):
31 def __call__(self, parser, namespace, values, option_string=None):
33 level -= 10 # e.g. logging.INFO -> logging.DEBUG
34 if level >= _logging.DEBUG:
38 # name -> (args, kwargs)
42 {'default':'/dev/comedi0',
43 'help':'path to comedi device file'}),
45 ['-s', '--subdevice'],
47 'help':'subdevice for analog input/output'}),
52 'help':'channel for analog input/output'}),
55 {'type':lambda x: [int(i) for i in x.split(',')],
57 'help':'comma-separated channels for analog input/output'}),
59 ['-a', '--analog-reference'],
61 'default':_constant.AREF.ground,
62 'type':lambda x: _constant.AREF.index_by_name(x),
63 'choices':_constant.AREF,
64 'help':'reference for analog input/output'}),
69 'help':'range for analog input/output'}),
71 ['-N', '--num-scans'],
74 'help':'number of input/output scans'}),
76 ['-F', '--frequency'],
78 'action':_SetFrequencyAction,
79 'help':'scan frequency in hertz'}),
83 'action':'store_const',
85 'help':'convert input to physical values before printing'}),
89 'action':'store_const',
91 'help':('use a memory-mapped reader/writer rather than '
92 'reading/writing the input/output subdevice directly')}),
96 'action':'store_const',
98 'help':('use a callback reader/writer rather than '
99 'reading/writing the input/output subdevice directly')}),
102 {'action':_IncrementVerbosityAction,
106 def parse_args(description, argnames, args=None):
108 >>> args = parse_args(
109 ... description='Parse a bunch of arguments',
110 ... argnames=['frequency'], args=['--frequency', '2'])
114 parser = _argparse.ArgumentParser(description=description)
115 for argument in argnames:
116 args_,kwargs = ARGUMENTS[argument]
117 parser.add_argument(*args_, **kwargs)
118 args = parser.parse_args(args=args)
119 if 'frequency' in argnames and not hasattr(args, 'period'):