hansken.tool
— Command line interaction
- run(args=None, using_parser=<hansken.py default parser>, with_connection=None, with_context=None, with_multi_context=None, error='raise', with_admin=None, **defaults)[source]
Run the command line tool, either as the command line interface defined by
hansken.py
itself, or running user code, piggybacking on all the command line argument parsing and resolving done byhansken.py
.Note
run
parses the command line by default. If user code does its own command line parsing, either pass applicable args torun
or useset_command
, passing the resultingargparse.ArgumentParser
torun
as using_parser after adding additional user arguments.Note
By default,
run
callsexit
on theargparse.ArgumentParser
in case of an error raised from whatever command is called (this includes user-provided commands). This call results in a CLI-like error message containing the exception message of the error that was caught. As the call toexit
raises aSystemExit
, any user code after the call torun
will not be executed. When the error argument torun
is set to'raise'
or the arguments parser sets verbose mode,exit
is not called, the exception is reraised instead.- Parameters:
args – arguments to the tool, a
list
ofstr
, orNone
, in which case args will be read from the command lineusing_parser – the
argparse.ArgumentParser
to be used, either the parser that processes the command line interface defined byhansken.py
itself or one that is the result ofset_command
with_connection – a user-defined
callable
to be run with aConnection
instance, passed as keyword argumentconnection
with_context – a user-defined
callable
to be run with aProjectContext
instance, passed as keyword argumentcontext
with_multi_context – a user-defined
callable
to be run with aMultiProjectContext
instance, passed as keyword argumentcontext
error – how to deal with an error raised from the selected command runner, defaults to propagating errors to the caller of
run
, set to'exit'
to callusing_parser.exit()
with a non-zero return code and failure messagedefaults – named arguments to be used as defaults when parsing the command line, matching the destinations of defined arguments (e.g.
project='a-project-uuid'
)
- Returns:
the return value of the command that was ultimately called (when
run
was called with either with_context or with_admin, the return value of that callable will be returned byrun
)
- create_argument_parser(requires_project=False, **kwargs)[source]
Creates an
ArgumentParser
to be used withrun
alongsidewith_context
orwith_admin
. This parser’s main feature is to not add a-h/--help
argument, which is attached to the main argument parser used byrun
.- Parameters:
requires_project – whether the parser should be fitted with a required positional argument
project
kwargs – any
ArgumentParser
constructor arguments (seeargparse
documentation)
- Returns:
an
ArgumentParser
that won’t clash withhansken.py
’s parent parser- Return type:
argparse.ArgumentParser