def test_group(self):
     p = Parser('p', 'p-description',
         Group('grp', 'grp-description',
             Arg('a'),
             Arg('b')))
     parse, args = p.parse_args('a b'.split())
     self.assertEqual(args.ns.a, 'a')
     self.assertEqual(args.ns.b, 'b')
    def test_positional_args(self):
        parser = Parser('', '',
            Arg(('-a', '--arg'), 'arg'))

        _, args = parser.parse_args('-a asdf'.split())
        self.assertEqual(args.ns.arg, 'asdf')

        _, args = parser.parse_args('--arg asdf'.split())
        self.assertEqual(args.ns.arg, 'asdf')
    def test_to_named_params(self):
        parser = Parser('name', 'description',
            Arg('a', 'arg a'),
            Arg('b', 'arg b'),
            Arg('c', 'arg c'))

        _, args = parser.parse_args('a b c'.split())
        self.assertEqual(args.ns.a, 'a')
        self.assertEqual(args.ns.b, 'b')
        self.assertEqual(args.ns.c, 'c')
    def test_to_dict(self):
        parser = Parser('name', 'description',
            Arg('a', 'arg a'),
            Arg('b', 'arg b'),
            Arg('c', 'arg c'))

        _, args = parser.parse_args('1 2 3'.split())
        dic = dict(args.ns)
        self.assertEqual(dic['a'], '1')
        self.assertEqual(dic['b'], '2')
        self.assertEqual(dic['c'], '3')
    def test_argparse_example(self):
        p = Parser(
            'cmd', 'Process some integers',
            Arg('integers', 'an integer for the accumulator',
                metavar='N', type=int, nargs='+'),
            Arg('--sum', 'sum the integers (default: find the max)',
                dest='accumulate', action='store_const',
                const=sum, default=max))
        parser, args = p.parse_args('1 2 3 4'.split())
        self.assertEqual(args.ns.accumulate(args.ns.integers), 4)

        parser, args = p.parse_args('1 2 3 4 --sum'.split())
        self.assertEqual(args.ns.accumulate(args.ns.integers), 10)
    def test_lazy(self):
        p = Parser('p', 'description',
                Arg('--b', 'description', types.Int.positive,
                    Lazy(lambda ns: ns.a)),
                Arg('a', 'description', types.Int.positive))

        parser, args = p.parse_args('1234'.split())
        self.assertEqual(args.ns.a, 1234)
        self.assertEqual(args.ns.b, 1234)

        parser, args = p.parse_args('--b 4321 1234'.split())
        self.assertEqual(args.ns.a, 1234)
        self.assertEqual(args.ns.b, 4321)
Exemple #7
0
def main():
    import os
    from terseparse import Parser, Arg, KW

    logging.getLogger().setLevel(logging.DEBUG)
    logging.basicConfig()

    p = Parser('dcmviewer', 'Display dicom series for viewing and editing ROIs',
            Arg('--version', 'show version', action='version',
                version='%(prog)s ({})'.format(__version__)),
            Arg('directory', 'Working directory to start the viewer in',
                default=os.path.abspath('.'), nargs='?'))
    _, args = p.parse_args()
    view(args.ns.directory)
    def test_subparser_common_args(self):
        p = Parser(
            'cmd', 'cmd-description',
            SubParsers('sp', 'sp-description', KW(dest='sp'),
                Arg('arg0', 'arg0-description'),
                Parser('p0', 'p0-description'),
                Parser('p1', 'p0-description',
                    Arg('p1-arg0', 'p1-arg0-description')),
                Arg('arg1', 'arg0-description')))

        parser, args = p.parse_args('p0 1 2'.split())
        self.assertEqual(args.ns.sp, 'p0')
        self.assertEqual(args.ns.arg0, '1')
        self.assertEqual(args.ns.arg1, '2')

        parser, args = p.parse_args('p1 a b c'.split())
        self.assertEqual(args.ns.sp, 'p1')
        self.assertEqual(args.ns.arg0, 'a')
        self.assertEqual(args.ns.arg1, 'b')
        self.assertEqual(args.ns.p1_arg0, 'c')
Exemple #9
0
#!/usr/bin/env python
from terseparse import Arg, KW, Parser, SubParsers, types

description = '''shows an example of using subparsers'''


parser = Parser(__file__, description,
        Arg('--arg0', 'arg 0', type=types.Int()),
        SubParsers('command', '', KW(dest='command'),
            Arg('common', 'this argument is common to all sub parsers', type=int),
            Parser('a', 'parser a',
                Arg('arg0', 'arg 0', type=int),
                Arg('arg1', 'arg 1', type=int),),
            Parser('b', 'parser b',
                Arg('arg0', 'arg 0', type=str),
                Arg('arg1', 'arg 1', type=str))))

_, args = parser.parse_args()
args.pprint()
Exemple #10
0
        return name


def fmt_slc(slc):
    return ','.join(map(str, slc))


p = Parser(
    'roi-edit', description,
    Arg('--dim',
        'dimension to flatten',
        DimEditType(),
        default=[],
        action='append'),
    Arg('--ndim', 'final number of dimensions', types.Int.positive,
        default=-1),
    Arg('--pretend',
        'do not commit any actions to disk, just pretend',
        action='store_true'),
    Arg('--rename',
        'old,new name mapping, multiple --rename args can be specified',
        RenameType(),
        default=[],
        action='append'), Arg('files', 'roi files to flatten', nargs='+'))


def main():
    _, args = p.parse_args()

    if args.ns.pretend:
        print('Pretending, file saving is disabled')
#!/usr/bin/env python
from terseparse import Parser, Arg

description = '''Process some integers
This example is adapted to terseparse syntax from the argument parser library
'''

p = Parser('cmd', description,
    Arg('integers', 'an integer for the accumulator',
        metavar='N', type=int, nargs='+'),
    Arg('--sum', 'sum the integers (default: find the max)',
        dest='accumulate', action='store_const',
        const=sum, default=max))

_, args = p.parse_args()
args.pprint()
import pdb; pdb.set_trace()
print(args.ns.accumulate(args.ns.integers))
Exemple #12
0
def inc_uid(uid):
    """Increment the UID"""
    ints = map(int, uid.split('.'))
    ints[-1] += 1
    return '.'.join(map(str, ints))


series_num_type = types.Int.positive | 'smart-pick' | 'pick'
output_series_num_type = types.Int.positive | 'auto' | 'add-100' | 'pick'


p = Parser('asl-rbf-to-dicom', description,
    Arg('--disable-cache-update', 'Disable updating of the dicom cache', action='store_true'),
    Arg('--verbose', 'Enable verbose printing', action='store_true'),
    Arg('--asl-series', 'ASL series number', series_num_type, default='smart-pick'),
    Arg('--output-series', 'Series number to save output as', output_series_num_type, default='pick'),
    Arg('--pretend', 'Disable writing', action='store_true'),
    Arg('--overwrite', 'Overwrite an existing file', action='store_true'),
    Arg('rbf', 'ASL rbf.mat file', RBFMatType.r),
    Arg('dicom-dir', 'A directory of dicom files from the series that the rbf.mat was derived from', types.Dir.rw))


def main():
    parser, args = p.parse_args()
    dcms = jtmri.dcm.read(args.ns.dicom_dir, disp=False)
    last_dcm = dcms[-1]
    if len(dcms) == 0:
        parser.error('No dicoms found in %s' % args.ns.dicom_dir)

    asl_series = get_asl_series(args.ns.asl_series, dcms)
    if asl_series is None: