def test_errors(self): p = Parser() p.int('a') try: p._process_command_line([]) except TypeError: self.fail()
def test_conditions(self): p = Parser() a = p.float('a') b = p.float('b') c = p.float('c').requires(a.or_(b)) self.assertRaises(DependencyError, p._process_command_line, ['--c', '9.2']) p._process_command_line(['--a', '11', '--c', '9.2']) p._process_command_line(['--b', '11', '--c', '9.2']) def create(): p = Parser() a = p.float('a') b = p.float('b') c = p.float('c').if_(a.or_(b)) return p create()._process_command_line(['--c', '9.2']) self.assertRaises(MissingRequiredArgumentError, create()._process_command_line, ['--a', '11']) create()._process_command_line(['--a', '11', '--c', '9.2']) self.assertRaises(MissingRequiredArgumentError, create()._process_command_line, ['--b', '11']) create()._process_command_line(['--b', '11', '--c', '9.2'])
def test_unspecified(self): p = Parser() arg = 'b' self.assertRaises(UnspecifiedArgumentError, p._process_command_line, ['--' + arg]) try: p._process_command_line(['--' + arg]) except UnspecifiedArgumentError as e: self.assertEqual(str(e), UnspecifiedArgumentError.message.format(arg))
def test_with(self): import sys sys.argv[1:] = ['--x', 'yes'] d = {'test_x': None} p = Parser(d) with p: p.str('x').shorthand('test-x')
def create(): p = Parser() p.all_if_any( p.int('x'), p.int('y'), p.int('z') ) return p
def create(): p = Parser() p.str('x').requires( p.str('y'), p.str('z'), p.str('w') ) return p
def create(): p = Parser() p.require_one( p.str('x'), p.str('y'), p.str('z') ) return p
def create(): p = Parser() p.at_least_one( p.str('x'), p.str('y'), p.str('z') ) return p
def create(): p = Parser({}) p.config('a') p.int('b') p.str('c') p.int('d') return p
def test_localize(self): p = Parser.with_locals() p.str('multi-word').requires(p.str('another-multi-word')) vals = p._process_command_line(['--multi-word', 'a', '--another-multi-word', 'b']) self.assertTrue('multi_word' in vals) self.assertFalse('multi-word' in vals) self.assertTrue('multi_word' in locals())
def test_conflicts(self): p = Parser() p.int('x').conflicts(p['x']) self.assertRaises(ConflictError, p._process_command_line, ['--x', '3']) def create(): p = Parser() p.int('x').conflicts(p.int('y')) return p self.assertRaises(ConflictError, create()._process_command_line, specify('x', 'y')) create()._process_command_line(specify('y')) try: create()._process_command_line(specify('x')) except: self.assertTrue(False)
def test_default(self): p = Parser() p.int('x').default(5) vals = p._process_command_line([]) self.assertEqual(vals['x'], 5) p = Parser() p.int('x').default(5) vals = p._process_command_line(['--x', '6']) self.assertEqual(vals['x'], 6)
def test_flag(self): p = Parser() p.flag('x') vals = p._process_command_line() self.assertTrue('x' in vals) self.assertFalse(vals['x']) p = Parser() p.flag('x') vals = p._process_command_line(['--x']) self.assertTrue(vals['x'])
def create(): p = Parser() p.int('x') p.int('y') p.int('z') p.only_one_if_any(*'xyz') return p
def create(): p = Parser() p.require_one( p.all_if_any( p.int('a'), p.int('b'), p.int('c'), ), p.only_one_if_any( p.int('d'), p.int('e'), p.int('f'), ), ) return p
def test_url(self): p = Parser() p.url('url').required() self.assertRaises(MissingRequiredArgumentError, p._process_command_line) vals = p._process_command_line(['--url', 'http://www.com']) p = Parser() p.url('url') self.assertRaises(FormatError, p._process_command_line, ['--url', '/www.com'])
def test_multiple(self): p = Parser() p.str('x') self.assertRaises(MultipleSpecifiedArgumentError, p._process_command_line, ['--x', '1', '--x', '2']) def create(): p = Parser() x = p.int('x').multiple() self.assertTrue(x != None) return p vals = create()._process_command_line(['--x', '1', '--x', '2']) self.assertEqual(sorted(vals['x']), [1, 2]) vals = create()._process_command_line(['--x', '1']) self.assertEqual(vals['x'], [1]) vals = create()._process_command_line([]) self.assertEqual(vals['x'], [None]) self.assertRaises(FormatError, create()._process_command_line, ['--x', '1', '--x', 'hello'])
def test_requires_and_default(self): p = Parser() p.int('a').default(3) b = p.int('b').requires(p['a']) b.requires(p['a']) self.assertEqual(len(b._getreqs()), 1) p._process_command_line(['--b', '5'])
def create(): p = Parser() a = p.int('a') b = p.int('b') p.int('c').requires(a.or_(b)) p.int('d').requires(a.and_(b)) return p
def test_basic(self): p = Parser() p.str('x') p.flag('y') vals = p._process_command_line([]) self.assertTrue(vals['x'] is None) # self.assertEqual(vals['x'], x) # self.assertEqual(vals['y'], y) self.assertFalse(vals['y'])
def test_requires(self): p = Parser() r = p.str('x') self.assertRaises(ValueError, r.requires, 'y') try: r.requires(p.str('y')) except ValueError: self.fail() p = Parser() y = p.int('y') p.int('x').requires(y) self.assertRaises(DependencyError, p._process_command_line, ['--x', '5'])
def main(bid_url=None): args = dict() with Parser(args) as p: p.flag('live') with Browser() as browser: _u = user.User() key = 'live' if args['live'] else 'demo' u = getattr(_u, key) e = Entry(u, browser, bid_url) e.browser.visit(base_url) e.login() while True: s = martingale_sequence() e.trade(s)
def create(): p = Parser() p.only_one_if_any( p.int('a'), p.int('b') ).requires( p.only_one_if_any( p.int('c'), p.int('d') ) ) return p
def test_env(self): import os os.environ['PORT'] = 'yes' p = Parser() p.int('port').environment() self.assertRaises(FormatError, p._process_command_line) os.environ['PORT'] = '9999' for port in ('Port', 'pORt', 'port', 'PORT'): p = Parser() p.int(port).environment() vals = p._process_command_line() self.assertEqual(vals[port], 9999) vals = p._process_command_line(['--%s' % port, '2222']) self.assertEqual(vals[port], 2222)
def test_one_required(self): def create(): p = Parser() p.require_one( p.str('x'), p.str('y'), p.str('z') ) return p create()._process_command_line(['--x', '1']) create()._process_command_line(['--y', '1']) create()._process_command_line(['--z', '1']) self.assertRaises(ConflictError, create()._process_command_line, ['--x', '1', '--y', '1']) self.assertRaises(ArgumentError, create()._process_command_line, []) p = Parser() p.int('a') p.int('b') p.require_one('a', 'b') p._process_command_line(['--b', '3'])
def create(): p = Parser() p.int('x').conflicts(p.int('y')) return p
def test_missing(self): p = Parser() p.int('x') self.assertRaises(MissingValueError, p._process_command_line, ['--x'])
def test_flag2(self): p = Parser() p.flag('x').requires(p.int('y')) p._process_command_line() self.assertRaises(DependencyError, p._process_command_line, ['--x'])
def create(): p = Parser() p.int('x') return p
def create(): p = Parser() p.enum('x', ('a', 'b', 'c')).multiple() return p
def test_required(self): p = Parser() p.str('x').required() self.assertRaises(MissingRequiredArgumentError, p._process_command_line, []) p = Parser() p.str('x').required() self.assertRaises(MissingRequiredArgumentError, p._process_command_line, []) p = Parser() y = p.str('y') z = p.str('z') x = p.str('x').unless(y.or_(z)) self.assertTrue(x != None) self.assertRaises(ArgumentError, p._process_command_line, []) p._process_command_line(['--y', 'a']) p._process_command_line(['--x', 'a']) p._process_command_line(['--x', 'a', '--y', 'b']) p._process_command_line(['--z', 'a'])