Example #1
0
def test_args_valuepre():
    spec = ExternalSpec('cmd', valuepre='=')
    c = Command(spec)
    args = spec.get_args_for(c(opt=1))
    assert ' '.join(args) == '--opt=1'
    args = spec.get_args_for(c(opt=[1, 2]))
    assert ' '.join(args) == '--opt=1 --opt=2'
Example #2
0
def test_args_underscore_positional():
    spec = ExternalSpec('cmd')
    c = Command(spec)
    assert spec.get_args_for(c(opt=True, _='foo')) == ['--opt', 'foo']
    assert spec.get_args_for(c('bar', opt=True,
                               _='foo')) == ['--opt', 'bar', 'foo']
    assert spec.get_args_for(c(opt=True,
                               _=['foo', 'bar'])) == ['--opt', 'foo', 'bar']
Example #3
0
def test_args_slice():
    spec = ExternalSpec('cmd')
    c = Command(spec)
    assert spec.get_args_for(c['foo']) == ['foo']
    assert spec.get_args_for(c['foo bar']) == ['foo', 'bar']
    assert spec.get_args_for(c[r'foo\ bar']) == ['foo bar']
    assert spec.get_args_for(c[r'foo\ \ bar']) == ['foo  bar']
    assert spec.get_args_for(c['foo\\\tbar']) == ['foo\tbar']
Example #4
0
def test_args_attr():
    spec = ExternalSpec('cmd')
    c = Command(spec)
    assert spec.get_args_for(c.a) == ['-a']
    assert spec.get_args_for(c.a.b.c) == ['-a', '-b', '-c']
    assert spec.get_args_for(c.a.b.c(10)) == ['-a', '-b', '-c', '10']
    assert spec.get_args_for(c.long) == ['--long']
    assert spec.get_args_for(c.long(10)) == ['--long', '10']
    assert spec.get_args_for(c.a.long.b) == ['-a', '--long', '-b']
    assert spec.get_args_for(c.hyphe_nate) == ['--hyphe-nate']

    with pytest.raises(AttributeError):
        c._foo  # underscode attributes are reserved
Example #5
0
def test_args_repeat():
    spec = ExternalSpec('cmd', repeat=',')
    c = Command(spec)
    assert spec.get_args_for(c(opt=[1, 2, 3])) == ['--opt', '1,2,3']

    spec = ExternalSpec('cmd', repeat=',', valuepre='=')
    c = Command(spec)
    assert spec.get_args_for(c(opt=[1, 2, 3])) == ['--opt=1,2,3']
Example #6
0
def args(cmd, **kwargs):
    spec = ExternalSpec(cmd, **kwargs)
    return spec.get_args_for(cmd)
Example #7
0
import pytest

from pysh.command import ExternalSpec
from pysh.dsl import Command

cmd = Command(ExternalSpec('dummy'))


def args(cmd, **kwargs):
    spec = ExternalSpec(cmd, **kwargs)
    return spec.get_args_for(cmd)


def test_args_short():
    c = cmd(a=True, b=True, c=False, d=10)
    assert args(c) == ['-a', '-b', '-d', '10']


def test_args_hyphenate():
    c = cmd('bar', o_bool=True, o_str='str', o_multi=[1, 2])
    assert args(c) == [
        '--o-bool', '--o-str', 'str', '--o-multi', '1', '--o-multi', '2', 'bar'
    ]


def test_args_valuepre():
    c = cmd(opt=1)
    assert args(c, value='=') == ['--opt=1']
    c = cmd(opt=[1, 2])
    assert args(c, value='=') == ['--opt=1', '--opt=2']
Example #8
0
def test_args_short():
    spec = ExternalSpec('cmd')
    c = Command(spec)(a=True, b=True, c=False, d=10)
    args = spec.get_args_for(c)
    assert args == ['-a', '-b', '-d', '10']
Example #9
0
def test_args_argspre():
    spec = ExternalSpec('cmd', argspre='--')
    c = Command(spec)
    assert spec.get_args_for(c('bar', 'baz',
                               opt=True)) == ['--opt', '--', 'bar', 'baz']
    assert spec.get_args_for(c('foo', 'bar')) == ['--', 'foo', 'bar']
Example #10
0
def test_args_hyphenate():
    spec = ExternalSpec('cmd')
    c = Command(spec)('bar', opt_bool=True, opt_str='str', opt_multi=[1, 2])
    args = spec.get_args_for(c)
    assert  ' '.join(args) \
        == '--opt-bool --opt-str str --opt-multi 1 --opt-multi 2 bar'