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
13 COMMON_ARGUMENT_GROUPS
= {
21 "help": "Use the specified commit message, or create it in your "
22 "$EDITOR if blank. Defaults to computed message.",
28 "action": "store_true",
30 "help": "Push despite a closed try tree",
36 "action": "store_true",
38 "help": "Submit changes for Lando to push to try.",
47 "help": "Save selection for future use with --preset.",
54 "help": "Load a saved selection.",
60 "action": "store_const",
61 "dest": "preset_action",
64 "help": "List available preset selections.",
70 "action": "store_const",
71 "dest": "preset_action",
74 "help": "Edit the preset file.",
82 "action": "store_true",
84 "help": "Use the full set of tasks as input to fzf (instead of "
89 ["-p", "--parameters"],
92 "help": "Use the given parameters.yml to generate tasks, "
93 "defaults to a default set of parameters",
99 NO_PUSH_ARGUMENT_GROUP
= [
103 "dest": "stage_changes",
104 "action": "store_true",
105 "help": "Locally stage changes created by this command but do not "
113 "action": "store_true",
114 "help": "Do not push to try as a result of running this command (if "
115 "specified this command will only print calculated try "
116 "syntax and selection info and not change files).",
122 class BaseTryParser(ArgumentParser
):
124 common_groups
= ["push", "preset"]
128 def __init__(self
, *args
, **kwargs
):
129 ArgumentParser
.__init
__(self
, *args
, **kwargs
)
131 group
= self
.add_argument_group("{} arguments".format(self
.name
))
132 for cli
, kwargs
in self
.arguments
:
133 group
.add_argument(*cli
, **kwargs
)
135 for name
in self
.common_groups
:
136 group
= self
.add_argument_group("{} arguments".format(name
))
137 arguments
= COMMON_ARGUMENT_GROUPS
[name
]
139 # Preset arguments are all mutually exclusive.
141 group
= group
.add_mutually_exclusive_group()
143 for cli
, kwargs
in arguments
:
144 group
.add_argument(*cli
, **kwargs
)
147 group_no_push
= group
.add_mutually_exclusive_group()
148 arguments
= NO_PUSH_ARGUMENT_GROUP
149 for cli
, kwargs
in arguments
:
150 group_no_push
.add_argument(*cli
, **kwargs
)
152 group
= self
.add_argument_group("task configuration arguments")
153 self
.task_configs
= {c
: all_task_configs
[c
]() for c
in self
.task_configs
}
154 for cfg
in self
.task_configs
.values():
155 cfg
.add_arguments(group
)
157 def validate(self
, args
):
158 if hasattr(args
, "message"):
159 if args
.message
== "editor":
160 if "EDITOR" not in os
.environ
:
162 "must set the $EDITOR environment variable to use blank --message"
165 with tempfile
.NamedTemporaryFile(mode
="r") as fh
:
166 subprocess
.call([os
.environ
["EDITOR"], fh
.name
])
167 args
.message
= fh
.read().strip()
169 if "{msg}" not in args
.message
:
170 args
.message
= "{}\n\n{}".format(args
.message
, "{msg}")
172 def parse_known_args(self
, *args
, **kwargs
):
173 args
, remainder
= ArgumentParser
.parse_known_args(self
, *args
, **kwargs
)
175 return args
, remainder