Compare commits
4 Commits
c588fc891e
...
709aec31f0
Author | SHA1 | Date | |
---|---|---|---|
|
709aec31f0 | ||
|
a3eb2f8e31 | ||
|
20dd942784 | ||
|
3f9cfbb8b4 |
@ -1,6 +1,6 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.5.0
|
rev: v4.6.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
@ -28,12 +28,12 @@ repos:
|
|||||||
hooks:
|
hooks:
|
||||||
- id: dead
|
- id: dead
|
||||||
- repo: https://github.com/asottile/pyupgrade
|
- repo: https://github.com/asottile/pyupgrade
|
||||||
rev: v3.15.1
|
rev: v3.15.2
|
||||||
hooks:
|
hooks:
|
||||||
- id: pyupgrade
|
- id: pyupgrade
|
||||||
args: [--py38-plus]
|
args: [--py38-plus]
|
||||||
- repo: https://github.com/hhatto/autopep8
|
- repo: https://github.com/hhatto/autopep8
|
||||||
rev: v2.0.4
|
rev: v2.1.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: autopep8
|
- id: autopep8
|
||||||
- repo: https://github.com/PyCQA/flake8
|
- repo: https://github.com/PyCQA/flake8
|
||||||
@ -42,6 +42,6 @@ repos:
|
|||||||
- id: flake8
|
- id: flake8
|
||||||
additional_dependencies: [flake8-encodings, flake8-warnings, flake8-builtins, flake8-print]
|
additional_dependencies: [flake8-encodings, flake8-warnings, flake8-builtins, flake8-print]
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v1.8.0
|
rev: v1.10.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
|
@ -2,12 +2,14 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import copy
|
import copy
|
||||||
|
import inspect
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import typing
|
import typing
|
||||||
|
import warnings
|
||||||
from argparse import Namespace
|
from argparse import Namespace
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
@ -152,11 +154,12 @@ class Setting:
|
|||||||
raise ValueError('names must be specified')
|
raise ValueError('names must be specified')
|
||||||
# We prefix the destination name used by argparse so that there are no conflicts
|
# We prefix the destination name used by argparse so that there are no conflicts
|
||||||
# Argument names will still cause an exception if there is a conflict e.g. if '-f' is defined twice
|
# Argument names will still cause an exception if there is a conflict e.g. if '-f' is defined twice
|
||||||
self.internal_name, setting_name, dest, self.flag = self.get_dest(group, names, dest)
|
self.internal_name, self.setting_name, dest, self.flag = self.get_dest(group, names, dest)
|
||||||
args: Sequence[str] = names
|
args: Sequence[str] = names
|
||||||
|
|
||||||
# We then also set the metavar so that '--config' in the group runtime shows as 'CONFIG' instead of 'RUNTIME_CONFIG'
|
# We then also set the metavar so that '--config' in the group runtime shows as 'CONFIG' instead of 'RUNTIME_CONFIG'
|
||||||
if not metavar and action not in ('store_true', 'store_false', 'count'):
|
if not metavar and action not in ('store_true', 'store_false', 'count', 'help', 'version'):
|
||||||
|
if not callable(action) or 'metavar' in inspect.signature(action).parameters.keys():
|
||||||
metavar = dest.upper()
|
metavar = dest.upper()
|
||||||
|
|
||||||
# If we are not a flag, no '--' or '-' in front
|
# If we are not a flag, no '--' or '-' in front
|
||||||
@ -174,7 +177,6 @@ class Setting:
|
|||||||
self.help = help
|
self.help = help
|
||||||
self.metavar = metavar
|
self.metavar = metavar
|
||||||
self.dest = dest
|
self.dest = dest
|
||||||
self.setting_name = setting_name
|
|
||||||
self.cmdline = cmdline
|
self.cmdline = cmdline
|
||||||
self.file = file
|
self.file = file
|
||||||
self.argparse_args = args
|
self.argparse_args = args
|
||||||
@ -686,12 +688,17 @@ def create_argparser(definitions: Definitions, description: str, epilog: str) ->
|
|||||||
argparser = argparse.ArgumentParser(
|
argparser = argparse.ArgumentParser(
|
||||||
description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter,
|
description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter,
|
||||||
)
|
)
|
||||||
for group in definitions.values():
|
|
||||||
for setting in group.v.values():
|
def get_current_group(setting: Setting) -> ArgParser:
|
||||||
if setting.cmdline:
|
|
||||||
argparse_args, argparse_kwargs = setting.to_argparse()
|
if not setting.group:
|
||||||
current_group: ArgParser = argparser
|
return argparser
|
||||||
if setting.group:
|
|
||||||
|
# Hard coded exception for positional arguments
|
||||||
|
# Ensures that the option shows at the top of the help output
|
||||||
|
if 'runtime' in setting.group.casefold() and setting.nargs == '*' and not setting.flag:
|
||||||
|
return argparser
|
||||||
|
|
||||||
if setting.group not in groups:
|
if setting.group not in groups:
|
||||||
if setting.exclusive:
|
if setting.exclusive:
|
||||||
groups[setting.group] = argparser.add_argument_group(
|
groups[setting.group] = argparser.add_argument_group(
|
||||||
@ -699,13 +706,17 @@ def create_argparser(definitions: Definitions, description: str, epilog: str) ->
|
|||||||
).add_mutually_exclusive_group()
|
).add_mutually_exclusive_group()
|
||||||
else:
|
else:
|
||||||
groups[setting.group] = argparser.add_argument_group(setting.group)
|
groups[setting.group] = argparser.add_argument_group(setting.group)
|
||||||
|
return groups[setting.group]
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.filterwarnings('ignore', message="'metavar", category=DeprecationWarning, module='argparse')
|
||||||
|
|
||||||
|
for group in definitions.values():
|
||||||
|
for setting in group.v.values():
|
||||||
|
if not setting.cmdline:
|
||||||
|
continue
|
||||||
|
argparse_args, argparse_kwargs = setting.to_argparse()
|
||||||
|
current_group: ArgParser = get_current_group(setting)
|
||||||
|
|
||||||
# Hard coded exception for positional arguments
|
|
||||||
# Ensures that the option shows at the top of the help output
|
|
||||||
if 'runtime' in setting.group.casefold() and setting.nargs == '*' and not setting.flag:
|
|
||||||
current_group = argparser
|
|
||||||
else:
|
|
||||||
current_group = groups[setting.group]
|
|
||||||
current_group.add_argument(*argparse_args, **argparse_kwargs)
|
current_group.add_argument(*argparse_args, **argparse_kwargs)
|
||||||
return argparser
|
return argparser
|
||||||
|
|
||||||
@ -1013,6 +1024,7 @@ def example_group(manager: Manager) -> None:
|
|||||||
manager.add_setting(
|
manager.add_setting(
|
||||||
'--verbose', '-v',
|
'--verbose', '-v',
|
||||||
default=False,
|
default=False,
|
||||||
|
metavar='nothing',
|
||||||
action=BooleanOptionalAction, # Added in Python 3.9
|
action=BooleanOptionalAction, # Added in Python 3.9
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user