Skip to content

Commit

Permalink
improve --json option help text
Browse files Browse the repository at this point in the history
  • Loading branch information
mjurbanski-reef committed Apr 3, 2024
1 parent 4c8abc9 commit 189d161
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
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.

0 comments on commit 189d161

Please sign in to comment.