mirror of
https://github.com/karma-riuk/crab.git
synced 2025-07-05 05:28:13 +02:00
added enumchoiceaction for easier enum in argparse
handling
This commit is contained in:
@ -2,7 +2,7 @@ from typing import Optional
|
|||||||
from dataset import Dataset, DatasetEntry, Selection
|
from dataset import Dataset, DatasetEntry, Selection
|
||||||
import argparse, os, re, click
|
import argparse, os, re, click
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from utils import prompt_yes_no
|
from utils import EnumChoicesAction, prompt_yes_no
|
||||||
|
|
||||||
HUNK_HEADER_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@')
|
HUNK_HEADER_REGEX = re.compile(r'^@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@')
|
||||||
|
|
||||||
@ -235,9 +235,10 @@ if __name__ == "__main__":
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-m",
|
"-m",
|
||||||
"--mode",
|
"--mode",
|
||||||
choices=[mode.value for mode in ValidationMode],
|
type=ValidationMode,
|
||||||
default='comment',
|
default=ValidationMode.COMMENT,
|
||||||
help="Validation mode: 'comment' to only check if comments suggest changes, 'refinement' to check both comment suggestions and implementation. Default is 'comment'",
|
action=EnumChoicesAction,
|
||||||
|
help=f"Validation mode: '{ValidationMode.COMMENT.value}' to only check if comments suggest changes, '{ValidationMode.REFINEMENT.value}' to check both comment suggestions and implementation. Default is '{ValidationMode.COMMENT.value}'",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--check-diff-relevance",
|
"--check-diff-relevance",
|
||||||
@ -249,6 +250,6 @@ if __name__ == "__main__":
|
|||||||
args.dataset,
|
args.dataset,
|
||||||
args.output,
|
args.output,
|
||||||
overwrite=args.overwrite,
|
overwrite=args.overwrite,
|
||||||
validation_mode=ValidationMode(args.mode),
|
validation_mode=args.mode,
|
||||||
check_diff_relevance=args.check_diff_relevance,
|
check_diff_relevance=args.check_diff_relevance,
|
||||||
)
|
)
|
||||||
|
22
utils.py
22
utils.py
@ -1,6 +1,7 @@
|
|||||||
import os, sys, logging, subprocess
|
import argparse
|
||||||
|
import os, enum, logging, subprocess
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Optional
|
from typing import Optional, Any, Sequence, Type, Union
|
||||||
from github.Commit import Commit
|
from github.Commit import Commit
|
||||||
from github.PaginatedList import PaginatedList
|
from github.PaginatedList import PaginatedList
|
||||||
from github.PullRequestComment import PullRequestComment
|
from github.PullRequestComment import PullRequestComment
|
||||||
@ -182,3 +183,20 @@ def prompt_yes_no(prompt: str, *, default: Optional[bool] = None) -> bool:
|
|||||||
return default
|
return default
|
||||||
else:
|
else:
|
||||||
print("Please enter 'y' or 'n'.")
|
print("Please enter 'y' or 'n'.")
|
||||||
|
|
||||||
|
|
||||||
|
class EnumChoicesAction(argparse.Action):
|
||||||
|
def __init__(self, *args, type: Type[enum.Enum], **kwargs) -> None:
|
||||||
|
super().__init__(*args, **kwargs, choices=[e.value for e in type])
|
||||||
|
self.enum = type
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self,
|
||||||
|
parser: argparse.ArgumentParser,
|
||||||
|
namespace: argparse.Namespace,
|
||||||
|
values: Union[str, Sequence[Any], None] = None,
|
||||||
|
option_string: Optional[str] = None,
|
||||||
|
) -> None:
|
||||||
|
if not isinstance(values, str):
|
||||||
|
raise TypeError
|
||||||
|
setattr(namespace, self.dest, self.enum(values))
|
||||||
|
Reference in New Issue
Block a user