Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve --json option help text #272

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions b2/_internal/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,23 @@ def lazy_get_description(cls, **kwargs):
return DescriptionGetter(cls, **kwargs)


class JSONOptionMixin(Described):
"""
Use ``--json`` to get machine-readable output.
Unless ``--json`` is used, the output is human-readable, and may change from one minor version to the next.
Therefore, for scripting, it is strongly encouraged to use ``--json``.
Data serialized in JSON is guaranteed to be stable across minor versions.
New fields may be added in new minor versions, but existing fields will not be removed or changed.
"""

@classmethod
def _setup_parser(cls, parser):
parser.add_argument(
'--json', action='store_true', help='output in JSON format to use in scripts'
)
super()._setup_parser(parser) # noqa


class DefaultSseMixin(Described):
"""
If you want server-side encryption for all of the files that are uploaded to a bucket,
Expand Down Expand Up @@ -2089,7 +2106,7 @@ def _run(self, args):
return 0


class ListBuckets(Command):
class ListBuckets(JSONOptionMixin, Command):
"""
Lists all of the buckets in the current account.
Expand All @@ -2100,19 +2117,13 @@ class ListBuckets(Command):
98c960fd1cb4390c5e0f0519 allPublic my-bucket
Alternatively, the ``--json`` option produces machine-readable output
similar (but not identical) to the server api response format.
{JSONOptionMixin}
Requires capability:
- **listBuckets**
"""

@classmethod
def _setup_parser(cls, parser):
parser.add_argument('--json', action='store_true')
super()._setup_parser(parser)

def _run(self, args):
return self.__class__.run_list_buckets(self, json_=args.json)

Expand Down Expand Up @@ -2325,7 +2336,7 @@ def get_b2_uri_from_arg(self, args: argparse.Namespace) -> B2URI:
raise NotImplementedError


class BaseLs(AbstractLsCommand, metaclass=ABCMeta):
class BaseLs(JSONOptionMixin, AbstractLsCommand, metaclass=ABCMeta):
"""
Using the file naming convention that ``/`` separates folder
names from their contents, returns a list of the files
Expand All @@ -2338,8 +2349,7 @@ class BaseLs(AbstractLsCommand, metaclass=ABCMeta):
name. Folders don't really exist in B2, so folders are
shown with ``-`` in each of the fields other than the name.
The ``--json`` option produces machine-readable output similar to
the server api response format.
{JSONOptionMixin}
The ``--replication`` option adds replication status
Expand All @@ -2353,7 +2363,6 @@ class BaseLs(AbstractLsCommand, metaclass=ABCMeta):
@classmethod
def _setup_parser(cls, parser):
parser.add_argument('--long', action='store_true')
parser.add_argument('--json', action='store_true')
parser.add_argument('--replication', action='store_true')
super()._setup_parser(parser)

Expand Down
2 changes: 2 additions & 0 deletions changelog.d/+json_flag.doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Document that `--json` flag guarantees a consistent output format.