예제 #1
0
def test_multi_extend():
    cmd = Command(lambda override: None, name='cmd')
    cmd.add('--override', char='o', multi=True)
    res = cmd.parse(['cmd', '-o', 'x=y', '-o', 'a=b'])

    assert res.flags['override'] == ['x=y', 'a=b']

    res = cmd.parse(['cmd'])
    assert res.flags['override'] == []

    res = cmd.parse(['cmd', '-o=x'])
    assert res.flags['override'] == ['x']
예제 #2
0
def test_post_posargs():
    cmd = Command(lambda posargs, post_posargs: None, name='cmd')

    res = cmd.parse(['cmd'])
    assert res.posargs == ()
    assert res.post_posargs == None
    # TODO: if this ^ isn't a useful signal, it would be more convenient to have the
    # behavior be the same as below

    res = cmd.parse(['cmd', '--'])
    assert res.posargs == ()
    assert res.post_posargs == ()
예제 #3
0
def test_char_missing_error():
    # testing required flags
    cmd = Command(lambda req_flag: None, name='cmd')
    cmd.add('--req-flag', char='-R', missing=ERROR)
    res = cmd.parse(['cmd', '--req-flag', 'val'])
    assert res.flags['req_flag'] == 'val'

    res = cmd.parse(['cmd', '-R', 'val'])
    assert res.flags['req_flag'] == 'val'

    with pytest.raises(ArgumentParseError, match='--req-flag'):
        cmd.parse(['cmd'])

    return
예제 #4
0
def test_flag_init():
    cmd = Command(lambda flag, part: None, name='cmd')

    with pytest.raises(ValueError,
                       match='cannot make an argument-less flag required'):
        cmd.add('--flag', missing=ERROR, parse_as=True)

    # test custom callable multi
    cmd.add(Flag('--part', multi=lambda flag, vals: ''.join(vals)))
    res = cmd.parse(['cmd', '--part', 'a', '--part', 'b'])
    assert res.flags['part'] == 'ab'

    with pytest.raises(ValueError,
                       match='multi expected callable, bool, or one of.*'):
        cmd.add('--badflag', multi='nope')