katsdpimager.arguments module

Utilities for processing and reconstructing command-line arguments.

class katsdpimager.arguments.SmartNamespace(**kwargs)

Bases: argparse.Namespace

Namespace that tracks whether arguments are default or not.

There are a few requirements for this to work: - All arguments must have a default, even if it is None. In other

words, do not set the default to argparse.SUPPRESS.

  • The default must have a meaningful equality comparison with the argument type.

  • It will break down if the default is a string but the argument type is not a string (in this case, argparse first sets it to the string value, then replaces it with the conversion of that string value).

Access to the information is provided by argument_changed() rather than via methods to avoid potential conflicts with command-line option names.

katsdpimager.arguments.argument_changed(namespace: katsdpimager.arguments.SmartNamespace, name: str)bool

True if the name has been set to more than one value.

This typically indicates that it was first set to a default, then set to a different value on the command line.

katsdpimager.arguments.unparse_args(args: katsdpimager.arguments.SmartNamespace, exclude: Iterable[str] = (), *, arg_handlers: Mapping[str, Callable[[str, Any], List[str]]] = {}, type_handlers: Mapping[type, Callable[[str, Any], List[str]]] = {})List[str]

Reconstruct an equivalent command line from parsed arguments.

  • args – Parsed arguments

  • exclude – Elements of args to skip. This should usually include at least the positional arguments.

  • arg_handlers – Special-case handling for particular arguments, mapping the name in args to a callable that takes the name and value and returns command-line arguments.

  • type_handlers – Like arg_handlers, but based on the type of the value rather than the destination name. There are standard type handlers for int, float, str, Quantity and bool. This is only used if the name is not in arg_handlers.


TypeError – If an argument has no handler.