Skip to content

Commit

Permalink
Mirror shtab, dcargs.conf.Suppress[], tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Sep 12, 2022
1 parent 1dcc10a commit 2aedb64
Show file tree
Hide file tree
Showing 16 changed files with 1,154 additions and 22 deletions.
11 changes: 7 additions & 4 deletions dcargs/_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ def _rule_generate_helptext(
lowered: LoweredArgumentDefinition,
) -> LoweredArgumentDefinition:
"""Generate helptext from docstring, argument name, default values."""

# If the suppress marker is attached, hide the argument.
if _markers.SUPPRESS in arg.field.markers:
return dataclasses.replace(lowered, help=argparse.SUPPRESS)

help_parts = []

docstring_help = arg.field.helptext
Expand Down Expand Up @@ -327,11 +332,9 @@ def _rule_set_name_or_flag(
elif lowered.action == "store_false":
name_or_flag = "--" + _strings.make_field_name(
[arg.prefix, "no-" + arg.field.name]
).replace("_", "-")
)
else:
name_or_flag = "--" + _strings.make_field_name(
[arg.prefix, arg.field.name]
).replace("_", "-")
name_or_flag = "--" + _strings.make_field_name([arg.prefix, arg.field.name])

return dataclasses.replace(
lowered,
Expand Down
6 changes: 3 additions & 3 deletions dcargs/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import warnings
from typing import Callable, Optional, Sequence, Type, TypeVar, Union, cast, overload

import shtab

from . import _argparse_formatter, _calling, _fields, _parsers, _strings, conf
from . import _argparse_formatter, _calling, _fields, _parsers
from . import _shtab as shtab
from . import _strings, conf

OutT = TypeVar("OutT")

Expand Down
31 changes: 22 additions & 9 deletions dcargs/_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ def from_callable(

if field.helptext is not None:
helptext_from_nested_class_field_name[
field.name
_strings.make_field_name([field.name])
] = field.helptext
else:
helptext_from_nested_class_field_name[
field.name
_strings.make_field_name([field.name])
] = _docstrings.get_callable_description(field.typ)
continue

Expand Down Expand Up @@ -207,20 +207,33 @@ def format_group_name(nested_field_name: str) -> str:
)
parser._action_groups = parser._action_groups[::-1]

# Add each argument.
# Add each argument group. Note that groups with only suppressed arguments won't
# be added.
for arg in self.args:
if arg.field.is_positional():
arg.add_argument(positional_group)
continue

if arg.prefix not in group_from_prefix:
if (
arg.lowered.help is not argparse.SUPPRESS
and arg.prefix not in group_from_prefix
):
group_from_prefix[arg.prefix] = parser.add_argument_group(
format_group_name(arg.prefix),
description=self.helptext_from_nested_class_field_name.get(
arg.prefix
),
)
arg.add_argument(group_from_prefix[arg.prefix])

# Add each argument.
for arg in self.args:
if arg.field.is_positional():
arg.add_argument(positional_group)
continue

if arg.prefix in group_from_prefix:
arg.add_argument(group_from_prefix[arg.prefix])
else:
# Suppressed argument: still need to add them, but they won't show up in
# the helptext so it doesn't matter which group.
assert arg.lowered.help is argparse.SUPPRESS
arg.add_argument(group_from_prefix[""])

# Create subparser tree.
if len(self.subparsers_from_name) > 0:
Expand Down
13 changes: 13 additions & 0 deletions dcargs/_shtab/LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2020-2021 Casper da Costa-Luis

Licensed under the Apache Licence, Version 2.0 (the "Licence");
you may not use this project except in compliance with the Licence.
You may obtain a copy of the Licence at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the Licence is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licence for the specific language governing permissions and
limitations under the Licence.
6 changes: 6 additions & 0 deletions dcargs/_shtab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Temporary copy of `shtab` that incorporates two PRs:

- https://github.com/iterative/shtab/pull/106
- https://github.com/iterative/shtab/pull/108

Can be removed when these are merged.
Loading

0 comments on commit 2aedb64

Please sign in to comment.