Skip to content

Commit

Permalink
Merge branch 'dev' into add-zsh-completion-support
Browse files Browse the repository at this point in the history
  • Loading branch information
zliang-akamai authored May 29, 2024
2 parents 4096017 + 0c8920e commit 07adbe2
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 140 deletions.
41 changes: 3 additions & 38 deletions linodecli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
print_help_plugins,
)
from .helpers import handle_url_overrides
from .output import OutputMode
from .output.output_handler import OutputMode
from .version import __version__

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

# output/formatting settings
if parsed.text:
cli.output_handler.mode = OutputMode.delimited
elif parsed.json:
cli.output_handler.mode = OutputMode.json
cli.output_handler.columns = "*"
elif parsed.markdown:
cli.output_handler.mode = OutputMode.markdown
elif parsed.ascii_table:
cli.output_handler.mode = OutputMode.ascii_table

if parsed.delimiter:
cli.output_handler.delimiter = parsed.delimiter
if parsed.pretty:
cli.output_handler.mode = OutputMode.json
cli.output_handler.pretty_json = True
cli.output_handler.columns = "*"
if parsed.no_headers:
cli.output_handler.headers = False
cli.output_handler.configure(parsed, cli.suppress_warnings)

if parsed.all_rows:
cli.pagination = False
elif parsed.format:
cli.output_handler.columns = parsed.format

cli.defaults = not parsed.no_defaults
cli.retry_count = 0
cli.no_retry = parsed.no_retry
cli.suppress_warnings = parsed.suppress_warnings

if parsed.all_columns or parsed.all:
if parsed.all and not cli.suppress_warnings:
print(
"WARNING: '--all' is a deprecated flag, "
"and will be removed in a future version. "
"Please consider use '--all-columns' instead."
)
cli.output_handler.columns = "*"

cli.page = parsed.page
cli.page_size = parsed.page_size
cli.debug_request = parsed.debug

cli.output_handler.suppress_warnings = parsed.suppress_warnings
cli.output_handler.disable_truncation = parsed.no_truncation
cli.output_handler.column_width = parsed.column_width
cli.output_handler.single_table = parsed.single_table
cli.output_handler.tables = parsed.table

if parsed.as_user and not skip_config:
cli.config.set_user(parsed.as_user)

Expand Down
90 changes: 7 additions & 83 deletions linodecli/arg_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

from linodecli import plugins
from linodecli.helpers import (
pagination_args_shared,
register_args_shared,
register_debug_arg,
register_pagination_args_shared,
)
from linodecli.output.helpers import register_output_args_shared


def register_args(parser):
Expand All @@ -41,106 +42,29 @@ def register_args(parser):
action="store_true",
help="Display information about a command, action, or the CLI overall.",
)
parser.add_argument(
"--text",
action="store_true",
help="Display text output with a delimiter (defaults to tabs).",
)
parser.add_argument(
"--delimiter",
metavar="DELIMITER",
type=str,
help="The delimiter when displaying raw output.",
)
parser.add_argument(
"--json", action="store_true", help="Display output as JSON."
)
parser.add_argument(
"--markdown",
action="store_true",
help="Display output in Markdown format.",
)
parser.add_argument(
"--ascii-table",
action="store_true",
help="Display output in an ASCII table.",
)
parser.add_argument(
"--pretty",
action="store_true",
help="If set, pretty-print JSON output.",
)
parser.add_argument(
"--no-headers",
action="store_true",
help="If set, does not display headers in output.",
)
parser.add_argument(
"--all",
action="store_true",
help=(
"Deprecated flag. An alias of '--all-columns', "
"scheduled to be removed in a future version."
),
)
parser.add_argument(
"--all-columns",
action="store_true",
help=(
"If set, displays all possible columns instead of "
"the default columns. This may not work well on some terminals."
),
)
parser.add_argument(
"--format",
metavar="FORMAT",
type=str,
help="The columns to display in output. Provide a comma-"
"separated list of column names.",
)

parser.add_argument(
"--no-defaults",
action="store_true",
help="Suppress default values for arguments. Default values "
"are configured on initial setup or with linode-cli configure",
)
parser.add_argument(
"--no-truncation",
action="store_true",
default=False,
help="Prevent the truncation of long values in command outputs.",
)

parser.add_argument(
"--no-retry",
action="store_true",
help="Skip retrying on common errors like timeouts.",
)
parser.add_argument(
"--single-table",
action="store_true",
help="Disable printing multiple tables for complex API responses.",
)
parser.add_argument(
"--table",
type=str,
action="append",
help="The specific table(s) to print in output of a command.",
)
parser.add_argument(
"--column-width",
type=int,
default=None,
help="Sets the maximum width of each column in outputted tables. "
"By default, columns are dynamically sized to fit the terminal.",
)

parser.add_argument(
"--version",
"-v",
action="store_true",
help="Prints version information and exits.",
)

pagination_args_shared(parser)
register_output_args_shared(parser)
register_pagination_args_shared(parser)
register_args_shared(parser)
register_debug_arg(parser)

Expand Down
2 changes: 1 addition & 1 deletion linodecli/baked/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from linodecli.baked.request import OpenAPIFilteringRequest, OpenAPIRequest
from linodecli.baked.response import OpenAPIResponse
from linodecli.output import OutputHandler
from linodecli.output.output_handler import OutputHandler
from linodecli.overrides import OUTPUT_OVERRIDES


Expand Down
8 changes: 4 additions & 4 deletions linodecli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

from openapi3 import OpenAPI

from .api_request import do_request, get_all_pages
from .baked import OpenAPIOperation
from .configuration import CLIConfig
from .output import OutputHandler, OutputMode
from linodecli.api_request import do_request, get_all_pages
from linodecli.baked import OpenAPIOperation
from linodecli.configuration import CLIConfig
from linodecli.output.output_handler import OutputHandler, OutputMode

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

Expand Down
2 changes: 1 addition & 1 deletion linodecli/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def handle_url_overrides(
).geturl()


def pagination_args_shared(parser: ArgumentParser):
def register_pagination_args_shared(parser: ArgumentParser):
"""
Add pagination related arguments to the given
ArgumentParser that may be shared across the CLI and plugins.
Expand Down
6 changes: 6 additions & 0 deletions linodecli/output/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Output formatting module for CLI and plugins.
"""

from .helpers import get_output_handler, register_output_args_shared
from .output_handler import OutputHandler
103 changes: 103 additions & 0 deletions linodecli/output/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""
Helpers for CLI output arguments and OutputHandler.
"""

from argparse import ArgumentParser, Namespace

from linodecli.output.output_handler import OutputHandler


def register_output_args_shared(parser: ArgumentParser):
"""
Add output formatting related arguments to the ArgumentParser.
"""
parser.add_argument(
"--text",
action="store_true",
help="Display text output with a delimiter (defaults to tabs).",
)
parser.add_argument(
"--delimiter",
metavar="DELIMITER",
type=str,
help="The delimiter when displaying raw output.",
)
parser.add_argument(
"--json", action="store_true", help="Display output as JSON."
)
parser.add_argument(
"--markdown",
action="store_true",
help="Display output in Markdown format.",
)

parser.add_argument(
"--ascii-table",
action="store_true",
help="Display output in an ASCII table.",
)
parser.add_argument(
"--pretty",
action="store_true",
help="If set, pretty-print JSON output.",
)
parser.add_argument(
"--no-headers",
action="store_true",
help="If set, does not display headers in output.",
)
parser.add_argument(
"--all",
action="store_true",
help=(
"Deprecated flag. An alias of '--all-columns', "
"scheduled to be removed in a future version."
),
)
parser.add_argument(
"--all-columns",
action="store_true",
help=(
"If set, displays all possible columns instead of "
"the default columns. This may not work well on some terminals."
),
)
parser.add_argument(
"--format",
metavar="FORMAT",
type=str,
help="The columns to display in output. Provide a comma-"
"separated list of column names.",
)
parser.add_argument(
"--no-truncation",
action="store_true",
default=False,
help="Prevent the truncation of long values in command outputs.",
)
parser.add_argument(
"--single-table",
action="store_true",
help="Disable printing multiple tables for complex API responses.",
)
parser.add_argument(
"--table",
type=str,
action="append",
help="The specific table(s) to print in output of a command.",
)
parser.add_argument(
"--column-width",
type=int,
default=None,
help="Sets the maximum width of each column in outputted tables. "
"By default, columns are dynamically sized to fit the terminal.",
)


def get_output_handler(parsed: Namespace, suppress_warnings: bool = False):
"""
Create a new OutputHandler and configure it with the parsed arguments.
"""
output_handler = OutputHandler()
output_handler.configure(parsed, suppress_warnings)
Loading

0 comments on commit 07adbe2

Please sign in to comment.