def test_multi_name_flag_docs(): def f(flag_name, another_flag): '''this is a cli :f flag-name: a flag's name :a another_flag: another flag ''' c = Command(f) assert '-f, --flag-name' in c.helptext() assert '-a, --another-flag' in c.helptext()
def test_incomplete_doc_parsing(): def fn(verbose: bool, hello): ''':l hello: say hello''' pass cmd = Command(fn) assert cmd.flags['l'] is not None assert cmd.flags['l'].name == 'hello' assert '-l, --hello' in cmd.helptext() assert 'say hello' in cmd.helptext() @command def fn2(verbose: bool, hello): ''':l hello: say hello''' assert '-l, --hello' in fn2.helptext() assert 'say hello' in fn2.helptext()
def testDocParsing(): def fn(verbose=False, pi=3.14159, tag: str = ''): '''fn is a function that has a multi-line description. :v verbose: : pi : this is the value of pi : t tag :this is a tag :return: None ''' parsed = _parse_flags_doc(fn.__doc__) assert 'verbose' in parsed assert parsed['verbose']['shorthand'] == 'v' assert 'pi' in parsed assert parsed['pi']['shorthand'] is None assert parsed['tag']['doc'] == "this is a tag" cmd = Command(fn) assert "fn is a function" in cmd._help assert "that has a multi-line description." in cmd._help assert len(cmd.flags) == 3 f = cmd.flags['verbose'] assert f.name == 'verbose' assert f.shorthand == 'v' assert f.type is bool assert f.value is False assert cmd.flags['pi'].type is float assert cmd.flags['pi'].value == 3.14159 assert cmd.flags['pi'].help == 'this is the value of pi' assert cmd.flags['tag'].type is str assert cmd.flags['tag'].help == 'this is a tag' assert cmd.flags['tag'].shorthand == 't' assert cmd.flags['tag'].value == '' assert cmd.flags['t'].name == 'tag' assert cmd.flags['t'].type is str assert cmd.flags['t'].help == 'this is a tag' assert cmd.flags['t'].shorthand == 't' assert cmd.flags['t'].value == '' assert cmd.flags['t'] == cmd.flags['tag'] assert id(cmd.flags['t']) == id(cmd.flags['tag']) assert cmd.flags['verbose'].has_default assert cmd.flags['verbose'].value == False assert cmd.flags['pi'].has_default assert cmd.flags['pi'].value == 3.14159 assert cmd.flags['tag'].has_default assert cmd.flags['tag'].value == '' hlp = cmd.helptext() assert 'return' not in hlp assert '--return' not in hlp for name, flag in cmd.flags.items(): assert name in hlp assert flag.help in hlp
def test_bad_doc(): def f1(verbose: bool): pass cmd = Command(f1) htext = cmd.helptext() assert 'Usage:\n f1 [options]' in htext assert '--verbose' in htext assert '-h, --help' in htext assert 'Get help.' in htext expected = 'Usage:\n f1 [options]\n\nOptions:\n --verbose \n -h, --help Get help.' # noqa assert htext and len(htext) > 5 got = cmd.helptext() assert expected.replace(' ', '') == got.replace(' ', '') def f2(): pass cmd = Command(f2) htext = cmd.helptext() assert htext and len(htext) > 5 assert EMPTY_HELP.format(name='f2') == htext