1 | """
|
2 | flag_util.py - API for builtin commands
|
3 | """
|
4 | from __future__ import print_function
|
5 |
|
6 | from _devbuild.gen.runtime_asdl import cmd_value, ProcArgs
|
7 | from core.error import e_usage
|
8 | from frontend import args
|
9 | from frontend import flag_spec
|
10 | from mycpp import mylib
|
11 |
|
12 | from typing import Tuple, Optional
|
13 |
|
14 | if mylib.PYTHON:
|
15 |
|
16 | def LookupFlagSpec(name):
|
17 | # type: (str) -> flag_spec._FlagSpec
|
18 | return flag_spec.FLAG_SPEC[name]
|
19 |
|
20 | def LookupFlagSpec2(name):
|
21 | # type: (str) -> flag_spec._FlagSpecAndMore
|
22 | return flag_spec.FLAG_SPEC_AND_MORE[name]
|
23 |
|
24 |
|
25 | def _DoesNotAccept(proc_args):
|
26 | # type: (Optional[ProcArgs]) -> None
|
27 | """ Copy from frontend/typed_args.py, to break dependency """
|
28 | if proc_args is not None:
|
29 | e_usage('got unexpected typed args', proc_args.typed_args.left)
|
30 |
|
31 |
|
32 | def ParseCmdVal(spec_name, cmd_val, accept_typed_args=False):
|
33 | # type: (str, cmd_value.Argv, bool) -> Tuple[args._Attributes, args.Reader]
|
34 |
|
35 | if not accept_typed_args:
|
36 | _DoesNotAccept(cmd_val.proc_args)
|
37 |
|
38 | #assert len(cmd_val.argv) == len(cmd_val.arg_locs), cmd_val
|
39 | arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
|
40 | arg_r.Next() # move past the builtin name
|
41 |
|
42 | spec = LookupFlagSpec(spec_name)
|
43 | return args.Parse(spec, arg_r), arg_r
|
44 |
|
45 |
|
46 | def ParseLikeEcho(spec_name, cmd_val):
|
47 | # type: (str, cmd_value.Argv) -> Tuple[args._Attributes, args.Reader]
|
48 |
|
49 | _DoesNotAccept(cmd_val.proc_args)
|
50 |
|
51 | arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
|
52 | arg_r.Next() # move past the builtin name
|
53 |
|
54 | spec = LookupFlagSpec(spec_name)
|
55 | return args.ParseLikeEcho(spec, arg_r), arg_r
|
56 |
|
57 |
|
58 | def Parse(spec_name, arg_r):
|
59 | # type: (str, args.Reader) -> args._Attributes
|
60 | """Parse argv using a given FlagSpec."""
|
61 | spec = LookupFlagSpec(spec_name)
|
62 | return args.Parse(spec, arg_r)
|
63 |
|
64 |
|
65 | def ParseMore(spec_name, arg_r):
|
66 | # type: (str, args.Reader) -> args._Attributes
|
67 | """Parse argv using a given FlagSpecAndMore."""
|
68 | spec = LookupFlagSpec2(spec_name)
|
69 | return args.ParseMore(spec, arg_r)
|