Fix boolean cli flags
This commit is contained in:
parent
83764c5aee
commit
c999325e8e
1 changed files with 25 additions and 7 deletions
|
@ -58,13 +58,31 @@ if __name__ == "__main__":
|
||||||
description = field.description
|
description = field.description
|
||||||
if field.default is not None and description is not None:
|
if field.default is not None and description is not None:
|
||||||
description += f" (default: {field.default})"
|
description += f" (default: {field.default})"
|
||||||
parser.add_argument(
|
base_type = get_base_type(field.annotation) if field.annotation is not None else str
|
||||||
f"--{name}",
|
list_type = contains_list_type(field.annotation)
|
||||||
dest=name,
|
if base_type is not bool:
|
||||||
nargs="*" if contains_list_type(field.annotation) else None,
|
parser.add_argument(
|
||||||
type=get_base_type(field.annotation) if field.annotation is not None else str,
|
f"--{name}",
|
||||||
help=description,
|
dest=name,
|
||||||
)
|
nargs="*" if list_type else None,
|
||||||
|
type=base_type,
|
||||||
|
help=description,
|
||||||
|
)
|
||||||
|
if base_type is bool:
|
||||||
|
parser.add_argument(
|
||||||
|
f"--{name}",
|
||||||
|
dest=name,
|
||||||
|
action="store_true",
|
||||||
|
help=f"Disable {description}",
|
||||||
|
default=field.default,
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
f"--no-{name}",
|
||||||
|
dest=name,
|
||||||
|
action="store_false",
|
||||||
|
help=f"Disable {description}",
|
||||||
|
default=field.default,
|
||||||
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
settings = Settings(**{k: v for k, v in vars(args).items() if v is not None})
|
settings = Settings(**{k: v for k, v in vars(args).items() if v is not None})
|
||||||
|
|
Loading…
Reference in a new issue