Skip to content

Commit 0c8920e

Browse files
Wrap output args helpers and create output module (#615)
1 parent b4e221f commit 0c8920e

File tree

11 files changed

+185
-139
lines changed

11 files changed

+185
-139
lines changed

linodecli/__init__.py

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
print_help_plugins,
3333
)
3434
from .helpers import handle_url_overrides
35-
from .output import OutputMode
35+
from .output.output_handler import OutputMode
3636
from .version import __version__
3737

3838
VERSION = __version__
@@ -64,54 +64,19 @@ def main(): # pylint: disable=too-many-branches,too-many-statements
6464
)
6565
parsed, args = register_args(parser).parse_known_args()
6666

67-
# output/formatting settings
68-
if parsed.text:
69-
cli.output_handler.mode = OutputMode.delimited
70-
elif parsed.json:
71-
cli.output_handler.mode = OutputMode.json
72-
cli.output_handler.columns = "*"
73-
elif parsed.markdown:
74-
cli.output_handler.mode = OutputMode.markdown
75-
elif parsed.ascii_table:
76-
cli.output_handler.mode = OutputMode.ascii_table
77-
78-
if parsed.delimiter:
79-
cli.output_handler.delimiter = parsed.delimiter
80-
if parsed.pretty:
81-
cli.output_handler.mode = OutputMode.json
82-
cli.output_handler.pretty_json = True
83-
cli.output_handler.columns = "*"
84-
if parsed.no_headers:
85-
cli.output_handler.headers = False
67+
cli.output_handler.configure(parsed, cli.suppress_warnings)
68+
8669
if parsed.all_rows:
8770
cli.pagination = False
88-
elif parsed.format:
89-
cli.output_handler.columns = parsed.format
9071

9172
cli.defaults = not parsed.no_defaults
9273
cli.retry_count = 0
9374
cli.no_retry = parsed.no_retry
9475
cli.suppress_warnings = parsed.suppress_warnings
95-
96-
if parsed.all_columns or parsed.all:
97-
if parsed.all and not cli.suppress_warnings:
98-
print(
99-
"WARNING: '--all' is a deprecated flag, "
100-
"and will be removed in a future version. "
101-
"Please consider use '--all-columns' instead."
102-
)
103-
cli.output_handler.columns = "*"
104-
10576
cli.page = parsed.page
10677
cli.page_size = parsed.page_size
10778
cli.debug_request = parsed.debug
10879

109-
cli.output_handler.suppress_warnings = parsed.suppress_warnings
110-
cli.output_handler.disable_truncation = parsed.no_truncation
111-
cli.output_handler.column_width = parsed.column_width
112-
cli.output_handler.single_table = parsed.single_table
113-
cli.output_handler.tables = parsed.table
114-
11580
if parsed.as_user and not skip_config:
11681
cli.config.set_user(parsed.as_user)
11782

linodecli/arg_helpers.py

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212

1313
from linodecli import plugins
1414
from linodecli.helpers import (
15-
pagination_args_shared,
1615
register_args_shared,
1716
register_debug_arg,
17+
register_pagination_args_shared,
1818
)
19+
from linodecli.output.helpers import register_output_args_shared
1920

2021

2122
def register_args(parser):
@@ -41,106 +42,29 @@ def register_args(parser):
4142
action="store_true",
4243
help="Display information about a command, action, or the CLI overall.",
4344
)
44-
parser.add_argument(
45-
"--text",
46-
action="store_true",
47-
help="Display text output with a delimiter (defaults to tabs).",
48-
)
49-
parser.add_argument(
50-
"--delimiter",
51-
metavar="DELIMITER",
52-
type=str,
53-
help="The delimiter when displaying raw output.",
54-
)
55-
parser.add_argument(
56-
"--json", action="store_true", help="Display output as JSON."
57-
)
58-
parser.add_argument(
59-
"--markdown",
60-
action="store_true",
61-
help="Display output in Markdown format.",
62-
)
63-
parser.add_argument(
64-
"--ascii-table",
65-
action="store_true",
66-
help="Display output in an ASCII table.",
67-
)
68-
parser.add_argument(
69-
"--pretty",
70-
action="store_true",
71-
help="If set, pretty-print JSON output.",
72-
)
73-
parser.add_argument(
74-
"--no-headers",
75-
action="store_true",
76-
help="If set, does not display headers in output.",
77-
)
78-
parser.add_argument(
79-
"--all",
80-
action="store_true",
81-
help=(
82-
"Deprecated flag. An alias of '--all-columns', "
83-
"scheduled to be removed in a future version."
84-
),
85-
)
86-
parser.add_argument(
87-
"--all-columns",
88-
action="store_true",
89-
help=(
90-
"If set, displays all possible columns instead of "
91-
"the default columns. This may not work well on some terminals."
92-
),
93-
)
94-
parser.add_argument(
95-
"--format",
96-
metavar="FORMAT",
97-
type=str,
98-
help="The columns to display in output. Provide a comma-"
99-
"separated list of column names.",
100-
)
45+
10146
parser.add_argument(
10247
"--no-defaults",
10348
action="store_true",
10449
help="Suppress default values for arguments. Default values "
10550
"are configured on initial setup or with linode-cli configure",
10651
)
107-
parser.add_argument(
108-
"--no-truncation",
109-
action="store_true",
110-
default=False,
111-
help="Prevent the truncation of long values in command outputs.",
112-
)
52+
11353
parser.add_argument(
11454
"--no-retry",
11555
action="store_true",
11656
help="Skip retrying on common errors like timeouts.",
11757
)
118-
parser.add_argument(
119-
"--single-table",
120-
action="store_true",
121-
help="Disable printing multiple tables for complex API responses.",
122-
)
123-
parser.add_argument(
124-
"--table",
125-
type=str,
126-
action="append",
127-
help="The specific table(s) to print in output of a command.",
128-
)
129-
parser.add_argument(
130-
"--column-width",
131-
type=int,
132-
default=None,
133-
help="Sets the maximum width of each column in outputted tables. "
134-
"By default, columns are dynamically sized to fit the terminal.",
135-
)
58+
13659
parser.add_argument(
13760
"--version",
13861
"-v",
13962
action="store_true",
14063
help="Prints version information and exits.",
14164
)
14265

143-
pagination_args_shared(parser)
66+
register_output_args_shared(parser)
67+
register_pagination_args_shared(parser)
14468
register_args_shared(parser)
14569
register_debug_arg(parser)
14670

linodecli/baked/operation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from linodecli.baked.request import OpenAPIFilteringRequest, OpenAPIRequest
2020
from linodecli.baked.response import OpenAPIResponse
21-
from linodecli.output import OutputHandler
21+
from linodecli.output.output_handler import OutputHandler
2222
from linodecli.overrides import OUTPUT_OVERRIDES
2323

2424

linodecli/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
from openapi3 import OpenAPI
1111

12-
from .api_request import do_request, get_all_pages
13-
from .baked import OpenAPIOperation
14-
from .configuration import CLIConfig
15-
from .output import OutputHandler, OutputMode
12+
from linodecli.api_request import do_request, get_all_pages
13+
from linodecli.baked import OpenAPIOperation
14+
from linodecli.configuration import CLIConfig
15+
from linodecli.output.output_handler import OutputHandler, OutputMode
1616

1717
METHODS = ("get", "post", "put", "delete")
1818

linodecli/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def handle_url_overrides(
3939
).geturl()
4040

4141

42-
def pagination_args_shared(parser: ArgumentParser):
42+
def register_pagination_args_shared(parser: ArgumentParser):
4343
"""
4444
Add pagination related arguments to the given
4545
ArgumentParser that may be shared across the CLI and plugins.

linodecli/output/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
Output formatting module for CLI and plugins.
3+
"""
4+
5+
from .helpers import get_output_handler, register_output_args_shared
6+
from .output_handler import OutputHandler

linodecli/output/helpers.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
Helpers for CLI output arguments and OutputHandler.
3+
"""
4+
5+
from argparse import ArgumentParser, Namespace
6+
7+
from linodecli.output.output_handler import OutputHandler
8+
9+
10+
def register_output_args_shared(parser: ArgumentParser):
11+
"""
12+
Add output formatting related arguments to the ArgumentParser.
13+
"""
14+
parser.add_argument(
15+
"--text",
16+
action="store_true",
17+
help="Display text output with a delimiter (defaults to tabs).",
18+
)
19+
parser.add_argument(
20+
"--delimiter",
21+
metavar="DELIMITER",
22+
type=str,
23+
help="The delimiter when displaying raw output.",
24+
)
25+
parser.add_argument(
26+
"--json", action="store_true", help="Display output as JSON."
27+
)
28+
parser.add_argument(
29+
"--markdown",
30+
action="store_true",
31+
help="Display output in Markdown format.",
32+
)
33+
34+
parser.add_argument(
35+
"--ascii-table",
36+
action="store_true",
37+
help="Display output in an ASCII table.",
38+
)
39+
parser.add_argument(
40+
"--pretty",
41+
action="store_true",
42+
help="If set, pretty-print JSON output.",
43+
)
44+
parser.add_argument(
45+
"--no-headers",
46+
action="store_true",
47+
help="If set, does not display headers in output.",
48+
)
49+
parser.add_argument(
50+
"--all",
51+
action="store_true",
52+
help=(
53+
"Deprecated flag. An alias of '--all-columns', "
54+
"scheduled to be removed in a future version."
55+
),
56+
)
57+
parser.add_argument(
58+
"--all-columns",
59+
action="store_true",
60+
help=(
61+
"If set, displays all possible columns instead of "
62+
"the default columns. This may not work well on some terminals."
63+
),
64+
)
65+
parser.add_argument(
66+
"--format",
67+
metavar="FORMAT",
68+
type=str,
69+
help="The columns to display in output. Provide a comma-"
70+
"separated list of column names.",
71+
)
72+
parser.add_argument(
73+
"--no-truncation",
74+
action="store_true",
75+
default=False,
76+
help="Prevent the truncation of long values in command outputs.",
77+
)
78+
parser.add_argument(
79+
"--single-table",
80+
action="store_true",
81+
help="Disable printing multiple tables for complex API responses.",
82+
)
83+
parser.add_argument(
84+
"--table",
85+
type=str,
86+
action="append",
87+
help="The specific table(s) to print in output of a command.",
88+
)
89+
parser.add_argument(
90+
"--column-width",
91+
type=int,
92+
default=None,
93+
help="Sets the maximum width of each column in outputted tables. "
94+
"By default, columns are dynamically sized to fit the terminal.",
95+
)
96+
97+
98+
def get_output_handler(parsed: Namespace, suppress_warnings: bool = False):
99+
"""
100+
Create a new OutputHandler and configure it with the parsed arguments.
101+
"""
102+
output_handler = OutputHandler()
103+
output_handler.configure(parsed, suppress_warnings)

0 commit comments

Comments
 (0)