Parses action from argv, or exits with usage info. The action is inferred from the first positional arg in argv[1:] (where argv[0] is interpreted as the CLI name). Args: argv: CLI arguments, possibly including --flags. options: Possible actions. default: Optiona
(
argv: Sequence[str], *, options: Sequence[str], default: Optional[str] = None
)
| 216 | |
| 217 | |
| 218 | def parse_action( |
| 219 | argv: Sequence[str], *, options: Sequence[str], default: Optional[str] = None |
| 220 | ) -> str: |
| 221 | """Parses action from argv, or exits with usage info. |
| 222 | |
| 223 | The action is inferred from the first positional arg in argv[1:] (where argv[0] is interpreted |
| 224 | as the CLI name). |
| 225 | |
| 226 | Args: |
| 227 | argv: CLI arguments, possibly including --flags. |
| 228 | options: Possible actions. |
| 229 | default: Optional default action if unable to infer action from argv. |
| 230 | |
| 231 | Returns: |
| 232 | The chosen action. |
| 233 | |
| 234 | Raises: |
| 235 | absl.app.UsageError: if an invalid action (or no action and default is None) is provided. |
| 236 | """ |
| 237 | assert default is None or default in options |
| 238 | action = None |
| 239 | for arg in argv[1:]: |
| 240 | arg = arg.strip() |
| 241 | if not arg.startswith("--"): |
| 242 | action = arg |
| 243 | break |
| 244 | if action not in options: |
| 245 | action = default |
| 246 | if action is None or action not in options: # No action nor default provided. |
| 247 | raise app.UsageError(f"Invalid action: {action}. Expected one of [{', '.join(options)}].") |
| 248 | return action |
| 249 | |
| 250 | |
| 251 | def send_signal(popen: subprocess.Popen, sig: int = signal.SIGKILL): |