1 | """ |
---|
2 | Extension functions for parsing sys.argv. |
---|
3 | |
---|
4 | Commands: |
---|
5 | |
---|
6 | get_args -- load all command-line arguments after the last -- |
---|
7 | into $arg1...$argN. |
---|
8 | |
---|
9 | """ |
---|
10 | |
---|
11 | import twill.utils |
---|
12 | |
---|
13 | def get_args(require=0): |
---|
14 | """ |
---|
15 | >> get_args [<require>] |
---|
16 | |
---|
17 | Load the command line arguments after the last '--' into $arg1...$argN, |
---|
18 | optionally requiring at least 'require' such arguments. |
---|
19 | """ |
---|
20 | from twill import commands, namespaces, shell, errors |
---|
21 | |
---|
22 | global_dict, local_dict = namespaces.get_twill_glocals() |
---|
23 | |
---|
24 | require = int(require) |
---|
25 | |
---|
26 | if len(shell.twillargs) < require: |
---|
27 | from twill.errors import TwillAssertionError |
---|
28 | raise TwillAssertionError("too few arguments; %d rather than %d" % \ |
---|
29 | (len(shell.twillargs), require,)) |
---|
30 | |
---|
31 | if shell.twillargs: |
---|
32 | for i, arg in enumerate(shell.twillargs): |
---|
33 | global_dict["arg%d" % (i + 1,)] = arg |
---|
34 | |
---|
35 | print>>commands.OUT, "get_args: loaded %d args as $arg1..$arg%d." % \ |
---|
36 | (i + 1, i + 1) |
---|
37 | else: |
---|
38 | print>>commands.OUT, "no arguments to parse!" |
---|