mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Implement BooleanOptionalAction
This commit is contained in:
parent
0ca0680165
commit
1f400d5964
@ -1,10 +1,62 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import configparser
|
import configparser
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Any, List, Optional, Sequence, Union
|
||||||
|
|
||||||
from ..output_dir import OnConflict, Redownload
|
from ..output_dir import OnConflict, Redownload
|
||||||
from ..version import NAME, VERSION
|
from ..version import NAME, VERSION
|
||||||
|
|
||||||
|
|
||||||
|
# TODO Replace with argparse version when updating to 3.9?
|
||||||
|
class BooleanOptionalAction(argparse.Action):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
option_strings: List[str],
|
||||||
|
dest: Any,
|
||||||
|
default: Any = None,
|
||||||
|
type: Any = None,
|
||||||
|
choices: Any = None,
|
||||||
|
required: Any = False,
|
||||||
|
help: Any = None,
|
||||||
|
metavar: Any = None,
|
||||||
|
):
|
||||||
|
if len(option_strings) != 1:
|
||||||
|
raise ValueError("There must be exactly one option string")
|
||||||
|
[self.name] = option_strings
|
||||||
|
if not self.name.startswith("--"):
|
||||||
|
raise ValueError(f"{self.name!r} doesn't start with '--'")
|
||||||
|
if self.name.startswith("--no-"):
|
||||||
|
raise ValueError(f"{self.name!r} starts with '--no-'")
|
||||||
|
|
||||||
|
options = [self.name, "--no-" + self.name[2:]]
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
options,
|
||||||
|
dest,
|
||||||
|
nargs=0,
|
||||||
|
default=default,
|
||||||
|
type=type,
|
||||||
|
choices=choices,
|
||||||
|
required=required,
|
||||||
|
help=help,
|
||||||
|
metavar=metavar,
|
||||||
|
)
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self,
|
||||||
|
parser: argparse.ArgumentParser,
|
||||||
|
namespace: argparse.Namespace,
|
||||||
|
values: Union[str, Sequence[Any], None],
|
||||||
|
option_string: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
if option_string and option_string in self.option_strings:
|
||||||
|
value = not option_string.startswith("--no-")
|
||||||
|
setattr(namespace, self.dest, value)
|
||||||
|
|
||||||
|
def format_usage(self) -> str:
|
||||||
|
return "--[no-]" + self.name[2:]
|
||||||
|
|
||||||
|
|
||||||
CRAWLER_PARSER = argparse.ArgumentParser(add_help=False)
|
CRAWLER_PARSER = argparse.ArgumentParser(add_help=False)
|
||||||
CRAWLER_PARSER_GROUP = CRAWLER_PARSER.add_argument_group(
|
CRAWLER_PARSER_GROUP = CRAWLER_PARSER.add_argument_group(
|
||||||
title="general crawler arguments",
|
title="general crawler arguments",
|
||||||
@ -103,10 +155,8 @@ PARSER.add_argument(
|
|||||||
help="custom working directory"
|
help="custom working directory"
|
||||||
)
|
)
|
||||||
PARSER.add_argument(
|
PARSER.add_argument(
|
||||||
"--explain", "-e",
|
"--explain",
|
||||||
# TODO Use argparse.BooleanOptionalAction after updating to 3.9
|
action=BooleanOptionalAction,
|
||||||
action="store_const",
|
|
||||||
const=True,
|
|
||||||
help="log and explain in detail what PFERD is doing"
|
help="log and explain in detail what PFERD is doing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user