Exemple #1
0
    def test_cmdline_parse(self):
        """
        Test the cmdline class for command line parsing
        """
        self.dbgfunc()
        c = U.cmdline(self.clargs)

        (o, a) = c.parse(['cmd', '-t', '--special'])
        self.expected(True, o.fribble)
        self.expected(True, o.special)
        self.expected(False, o.debug)

        (o, a) = c.parse(['cmd', '--test'])
        self.expected(True, o.fribble)
        self.expected(False, o.special)
        self.expected(False, o.debug)

        (o, a) = c.parse(['cmd', '-s'])
        self.expected(False, o.fribble)
        self.expected(True, o.special)
        self.expected(False, o.debug)

        (o, a) = c.parse(['cmd'])
        self.expected(False, o.fribble)
        self.expected(False, o.special)
        self.expected(False, o.debug)
Exemple #2
0
    def test_cmdline_default_type(self):
        """
        Have the cmdline class set defaults for us

            Attribute       Default       Override Argument
                action          'store'       default_action
                default         None          default_default
                dest            name          default_dest
                type            string        default_type

        So, for example, the default action is 'store', but you can override
        this by passing, say, default_action='store_true' to the cmdline
        constructor.

        You can also specify the action for a specific option in the dict
        defining that option.
        """
        pytest.debug_func()
        c = U.cmdline(simple_arg_list(),
                      default_type=float)
        try:
            (o, a) = c.parse(['cmd',
                              '--first', 7.3,
                              '--second', 9.452,
                              ])
        except SystemExit:
            pass

        assert o.first == 7.3
        assert o.second == [9.452]
        assert o.third is False
        assert o.forward is None
Exemple #3
0
    def test_cmdline_attr(self):
        """
        Test the cmdline class for command line parsing
        """
        self.dbgfunc()
        c = U.cmdline(self.clargs)

        self.assertTrue(hasattr(c, 'p'),
                        "Expected attribute 'p' on cmdline object")
Exemple #4
0
    def test_cmdline_long_opts(self):
        """
        Test the cmdline class for command line parsing
        """
        self.dbgfunc()
        c = U.cmdline(self.clargs)

        self.exp_in_got('--debug', c.p._long_opt)
        self.exp_in_got('--test', c.p._long_opt)
        self.exp_in_got('--special', c.p._long_opt)
Exemple #5
0
def test_cmdline_help(capsys):
    """
    Test the cmdline class for command line parsing
    """
    pytest.debug_func()
    c = U.cmdline(cmdline_arg_list())
    try:
        c.parse(['cmd', '-h'])
    except SystemExit:
        pass
    o, e = capsys.readouterr()
    assert 'Usage:' in o
    assert '-h, --help     show this help message and exit' in o
    assert '-t, --test     help string' in o
    assert '-s, --special  help string' in o
    assert '-d, --debug    run under the debugger' in o
Exemple #6
0
def make_option_parser(argv):
    """
    Build a parser to understand the command line options.
    """
    c = U.cmdline([{'opts': ['-c', '--copy'],
                    'action': 'store',
                    'type': 'string',
                    'dest': 'copy',
                    'default': '',
                    'help': 'copy this code to <dir>',
                    },
                   {'opts': ['-d', '--day'],
                    'action': 'store_true',
                    'dest': 'dayflag',
                    'default': False,
                    'help': 'report each day separately',
                    },
                   {'opts': ['-e', '--end'],
                    'action': 'store',
                    'type': 'string',
                    'dest': 'end_ymd',
                    'default': '',
                    'help': 'end date for report YYYY.MMDD',
                    },
                   {'opts': ['-f', '--file'],
                    'action': 'store',
                    'type': 'string',
                    'dest': 'filename',
                    'default': default_input_filename(),
                    'help': 'timelog to read',
                    },
                   {'opts': ['-g', '--debug'],
                    'action': 'store_true',
                    'dest': 'debug',
                    'default': False,
                    'help': 'run the debugger',
                    },
                   {'opts': ['-l', '--last'],
                    'action': 'store_true',
                    # 'type': 'string',
                    'dest': 'lastweek',
                    'default': False,
                    'help': 'report the last week',
                    },
                   {'opts': ['-m', '--match'],
                    'action': 'store',
                    'type': 'string',
                    'dest': 'match_regexp',
                    'default': '',
                    'help': 'regexp to match',
                    },
                   {'opts': ['-s', '--start'],
                    'action': 'store',
                    'type': 'string',
                    'dest': 'start_ymd',
                    'default': '',
                    'help': 'start date for report YYYY.MMDD',
                    },
                   {'opts': ['-S', '--since'],
                    'action': 'store',
                    'dest': 'since',
                    'type': 'string',
                    'default': '',
                    'help': 'report date YYYY.MMDD through end of file',
                    },
                   {'opts': ['-w', '--week'],
                    'action': 'store',
                    'dest': 'weektype',
                    'type': 'string',
                    'default': '',
                    'help': 'one of [fcMTWtFsS]',
                    },
                   {'opts': ['-D', '--doc'],
                    'action': 'store_true',
                    'dest': 'help',
                    'default': False,
                    'help': 'show script documentation',
                    },
                   {'opts': ['-p', '--pkg'],
                    'action': 'store',
                    'type': 'string',
                    'dest': 'tarball',
                    'default': '',
                    'help': 'package this code to <filename>',
                    },
                   {'opts': ['-v', '--verbose'],
                    'action': 'store_true',
                    'dest': 'verbose',
                    'default': False,
                    'help': 'display debugging info',
                    },
                   ])
    (o, a) = c.parse(argv)
    return(o, a)