COT.cli
module¶
CLI entry point for the Common OVF Tool (COT) suite.
Classes
CLI |
Command-line user interface for COT. |
-
class
CLI
(terminal_width=None)[source]¶ Bases:
COT.ui_shared.UI
Command-line user interface for COT.
confirm
Prompt user to confirm the requested operation. create_parser
Create parser
object for globalcot
command.create_subparsers
Populate the CLI sub-parsers for all known submodules. fill_examples
Pretty-print a set of usage examples. fill_usage
Pretty-print a list of usage strings for a COT subcommand. formatter
Create formatter for log output. get_input
Prompt the user to enter a string. get_password
Get password string from the user. main
Main worker function for COT when invoked from the CLI. parse_args
Parse the given CLI arguments into a namespace object. run
Parse the given CLI args then run. set_verbosity
Enable logging and/or change the logging verbosity level. terminal_width
The width of the terminal in columns. -
add_subparser
(title, parent=None, aliases=None, lookup_prefix='', **kwargs)[source]¶ Create a subparser under the specified parent.
Parameters: - title (str) – Canonical keyword for this subparser
- parent (object) – Subparser grouping object returned by
ArgumentParser.add_subparsers()
- aliases (list) – Aliases for
title
. Only used in Python 3.x. - lookup_prefix (str) – String to prepend to
title
and each alias inaliases
for lookup purposes.
-
confirm
(prompt)[source]¶ Prompt user to confirm the requested operation.
Auto-accepts if
force
is set toTrue
.Parameters: prompt (str) – Message to prompt the user with Returns: True
(user confirms acceptance) orFalse
(user declines)
-
create_parser
()[source]¶ Create
parser
object for globalcot
command.Includes a number of globally applicable CLI options.
-
create_subparsers
()[source]¶ Populate the CLI sub-parsers for all known submodules.
Creates an instance of each
COTGenericSubmodule
subclass, then callscreate_subparser()
for each.
-
fill_examples
(example_list)[source]¶ Pretty-print a set of usage examples.
>>> fill_examples([ ... ("Deploy to vSphere/ESXi server 192.0.2.100 with credentials" ... " admin/admin, creating a VM named 'test_vm' from foo.ova.", ... 'cot deploy foo.ova esxi 192.0.2.100 -u admin -p admin' ... ' -n test_vm'), ... ("Deploy to vSphere/ESXi server 192.0.2.100, with username" ... " admin (prompting the user to input a password at runtime)," ... " creating a VM based on profile '1CPU-2.5GB' in foo.ova.", ... 'cot deploy foo.ova esxi 192.0.2.100 -u admin -c 1CPU-2.5GB') ... ]) Examples: Deploy to vSphere/ESXi server 192.0.2.100 with credentials admin/admin, creating a VM named 'test_vm' from foo.ova. cot deploy foo.ova esxi 192.0.2.100 -u admin -p admin \ -n test_vm Deploy to vSphere/ESXi server 192.0.2.100, with username admin (prompting the user to input a password at runtime), creating a VM based on profile '1CPU-2.5GB' in foo.ova. cot deploy foo.ova esxi 192.0.2.100 -u admin -c 1CPU-2.5GB
Parameters: example_list (list) – List of (description, CLI example) tuples. Returns: Examples wrapped appropriately to the terminal_width()
value. CLI examples will be wrapped with backslashes and a hanging indent.
-
fill_usage
(subcommand, usage_list)[source]¶ Pretty-print a list of usage strings for a COT subcommand.
Automatically prepends a
cot subcommand --help
usage string to the provided list.>>> fill_usage('add-file', ["FILE PACKAGE [-o OUTPUT] [-f FILE_ID]"]) cot add-file --help cot add-file FILE PACKAGE [-o OUTPUT] [-f FILE_ID]
Parameters: - subcommand (str) – Subcommand name/keyword
- usage_list (list) – List of usage strings for this subcommand.
Returns: String containing all usage strings, each appropriately wrapped to the
terminal_width()
value.
-
formatter
(verbosity=20)[source]¶ Create formatter for log output.
We offer different (more verbose) formatting when debugging is enabled, hence this need.
Parameters: verbosity – Logging level as defined by logging
.Returns: Formatter object for use with logging
.Return type: instance of colorlog.ColoredFormatter
-
get_input
(prompt, default_value)[source]¶ Prompt the user to enter a string.
Auto-inputs the
default_value
ifforce
is set toTrue
.Parameters: - prompt (str) – Message to prompt the user with
- default_value (str) – Default value to input if the user simply
hits Enter without entering a value, or if
force
.
Returns: Input value
Return type: str
-
get_password
(username, host)[source]¶ Get password string from the user.
Parameters: - username (str) – Username the password is associated with
- host (str) – Host the password is associated with
Raises: InvalidInputError – if
force
isTrue
(as there is no “default” password value)
-
main
(args)[source]¶ Main worker function for COT when invoked from the CLI.
- Calls
set_verbosity()
with the appropriate verbosity level derived from the args. - Looks up the appropriate
COTGenericSubmodule
instance corresponding to the subcommand that was invoked. - Converts
args
to a dict and callsset_value()
for each arg/value in the dict. - Calls
run()
followed byfinished()
. - Catches various exceptions and handles them appropriately.
Parameters: args – Parser namespace object returned from parse_args()
.Return type: int Returns: Exit code for the COT executable. - 0 on successful completion
- 1 on runtime error
- 2 on input error (parser error,
InvalidInputError
, etc.)
- Calls
-
parse_args
(argv)[source]¶ Parse the given CLI arguments into a namespace object.
Parameters: argv (list) – List of CLI arguments, not including argv0 Returns: Parser namespace object
-
run
(argv)[source]¶ Parse the given CLI args then run.
Calls
parse_args()
followed bymain()
.Parameters: argv (list) – The CLI argv value (not including argv[0]) Returns: Return code from main()
-
set_verbosity
(level)[source]¶ Enable logging and/or change the logging verbosity level.
Will call
formatter()
and associate the resulting formatter with logging.Parameters: level – Logging level as defined by logging
-
terminal_width
¶ The width of the terminal in columns.
-