-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
ParserSpec
-> argparse.ArgumentParser
generator
- Loading branch information
1 parent
78e7344
commit 669c457
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import argparse | ||
from typing import Any, Dict, Type, TypeVar | ||
|
||
from httpie.cli.argparser import HTTPieArgumentParser | ||
|
||
from httpie.cli.opts import ParserSpec, Qualifiers | ||
from httpie.cli.utils import LazyChoices | ||
|
||
ParserType = TypeVar("ParserType", bound=Type[argparse.ArgumentParser]) | ||
|
||
|
||
ARGPARSE_QUALIFIER_MAP = { | ||
Qualifiers.OPTIONAL: argparse.OPTIONAL, | ||
Qualifiers.SUPPRESS: argparse.SUPPRESS, | ||
Qualifiers.ZERO_OR_MORE: argparse.ZERO_OR_MORE, | ||
} | ||
|
||
|
||
def map_qualifiers( | ||
configuration: Dict[str, Any], qualifier_map: Dict[Qualifiers, Any] | ||
) -> Dict[str, Any]: | ||
return { | ||
key: qualifier_map[value] if isinstance(value, Qualifiers) else value | ||
for key, value in configuration.items() | ||
} | ||
|
||
|
||
def to_argparse( | ||
abstract_options: ParserSpec, | ||
parser_type: ParserType = HTTPieArgumentParser, | ||
) -> ParserType: | ||
concrete_parser = parser_type( | ||
prog=abstract_options.program, | ||
description=abstract_options.description, | ||
epilog=abstract_options.epilog, | ||
) | ||
concrete_parser.register("action", "lazy_choices", LazyChoices) | ||
|
||
for abstract_group in abstract_options.groups: | ||
concrete_group = concrete_parser.add_argument_group( | ||
title=abstract_group.name, description=abstract_group.description | ||
) | ||
if abstract_group.mutually_exclusive: | ||
concrete_group = concrete_group.add_mutually_exclusive_group(required=False) | ||
|
||
for abstract_argument in abstract_group.arguments: | ||
concrete_group.add_argument( | ||
*abstract_argument.aliases, | ||
**map_qualifiers( | ||
abstract_argument.configuration, ARGPARSE_QUALIFIER_MAP | ||
) | ||
) | ||
|
||
return concrete_parser |