1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 from argparse
import ArgumentParser
11 from .task_config
import all_task_configs
14 COMMON_ARGUMENT_GROUPS
= {
22 "help": "Use the specified commit message, or create it in your "
23 "$EDITOR if blank. Defaults to computed message.",
30 "action": "store_false",
31 "help": "Do not push to try as a result of running this command (if "
32 "specified this command will only print calculated try "
33 "syntax and selection info).",
39 "action": "store_true",
41 "help": "Push despite a closed try tree",
50 "help": "Save selection for future use with --preset.",
57 "help": "Load a saved selection.",
63 "action": "store_const",
64 "dest": "preset_action",
67 "help": "List available preset selections.",
73 "action": "store_const",
74 "dest": "preset_action",
77 "help": "Edit the preset file.",
85 "action": "store_true",
87 "help": "Use the full set of tasks as input to fzf (instead of "
92 ["-p", "--parameters"],
95 "help": "Use the given parameters.yml to generate tasks, "
96 "defaults to a default set of parameters",
103 class BaseTryParser(ArgumentParser
):
105 common_groups
= ["push", "preset"]
109 def __init__(self
, *args
, **kwargs
):
110 ArgumentParser
.__init
__(self
, *args
, **kwargs
)
112 group
= self
.add_argument_group("{} arguments".format(self
.name
))
113 for cli
, kwargs
in self
.arguments
:
114 group
.add_argument(*cli
, **kwargs
)
116 for name
in self
.common_groups
:
117 group
= self
.add_argument_group("{} arguments".format(name
))
118 arguments
= COMMON_ARGUMENT_GROUPS
[name
]
120 # Preset arguments are all mutually exclusive.
122 group
= group
.add_mutually_exclusive_group()
124 for cli
, kwargs
in arguments
:
125 group
.add_argument(*cli
, **kwargs
)
127 group
= self
.add_argument_group("task configuration arguments")
128 self
.task_configs
= {c
: all_task_configs
[c
]() for c
in self
.task_configs
}
129 for cfg
in self
.task_configs
.values():
130 cfg
.add_arguments(group
)
132 def validate(self
, args
):
133 if hasattr(args
, "message"):
134 if args
.message
== "editor":
135 if "EDITOR" not in os
.environ
:
137 "must set the $EDITOR environment variable to use blank --message"
140 with tempfile
.NamedTemporaryFile(mode
="r") as fh
:
141 subprocess
.call([os
.environ
["EDITOR"], fh
.name
])
142 args
.message
= fh
.read().strip()
144 if "{msg}" not in args
.message
:
145 args
.message
= "{}\n\n{}".format(args
.message
, "{msg}")
147 def parse_known_args(self
, *args
, **kwargs
):
148 args
, remainder
= ArgumentParser
.parse_known_args(self
, *args
, **kwargs
)
150 return args
, remainder