Skip to content

Commit

Permalink
Improved help string and updated demo
Browse files Browse the repository at this point in the history
  • Loading branch information
swansonk14 committed Aug 23, 2019
1 parent addfe78 commit 986fa53
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
15 changes: 9 additions & 6 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ def add_one(num: int) -> int:
# ----- ArgumentParser -----

parser = ArgumentParser()
parser.add_argument('--rnn', type=str, required=True)
parser.add_argument('--hidden_size', type=int, default=300)
parser.add_argument('--dropout', type=float, default=0.2)
parser.add_argument('--rnn', type=str, required=True,
help='RNN type')
parser.add_argument('--hidden_size', type=int, default=300,
help='Hidden size')
parser.add_argument('--dropout', type=float, default=0.2,
help='Dropout probability')


args = parser.parse_args()
Expand All @@ -24,9 +27,9 @@ def add_one(num: int) -> int:
# ----- Tap -----

class MyTap(Tap):
rnn: str
hidden_size: int = 300
dropout: float = 0.2
rnn: str # RNN type
hidden_size: int = 300 # Hidden size
dropout: float = 0.2 # Dropout probability


args = MyTap().parse_args()
Expand Down
40 changes: 24 additions & 16 deletions tap/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,35 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:
if hasattr(self, variable):
kwargs['default'] = kwargs.get('default', getattr(self, variable))

# Set required if option arg
if is_option_arg(*name_or_flags) and variable != 'help':
kwargs['required'] = kwargs.get('required', not hasattr(self, variable))

# Set help if necessary
if 'help' not in kwargs:
kwargs['help'] = '('

# Type
if variable in self._annotations:
kwargs['help'] += type_to_str(self._annotations[variable]) + ', '

# Required/default
if kwargs.get('required', False):
kwargs['help'] += 'required'
else:
kwargs['help'] += f'default={kwargs.get("default", None)}'

kwargs['help'] += ')'

# Description
if variable in self.class_variables:
kwargs['help'] += ' ' + self.class_variables[variable]['comment']

# Set other kwargs where not provided
if variable in self._annotations:
# Get type annotation
var_type = self._annotations[variable]

# Set required if option arg
if is_option_arg(*name_or_flags):
kwargs['required'] = kwargs.get('required', not hasattr(self, variable))

# Set help if necessary
if 'help' not in kwargs:
# Add type and default
kwargs['help'] = '(' + type_to_str(var_type)
if 'default' in kwargs:
kwargs['help'] += f', default={kwargs["default"]}'
kwargs['help'] += ')'

# Add description
if variable in self.class_variables:
kwargs['help'] += ' ' + self.class_variables[variable]['comment']

# If type is not explicitly provided, set it if it's one of our supported default types
if 'type' not in kwargs:
if var_type not in SUPPORTED_DEFAULT_TYPES:
Expand Down

0 comments on commit 986fa53

Please sign in to comment.