mirror of
https://github.com/karma-riuk/crab.git
synced 2025-07-04 21:28:12 +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
|
||||
import argparse, os, re, click
|
||||
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+)? @@')
|
||||
|
||||
@ -235,9 +235,10 @@ if __name__ == "__main__":
|
||||
parser.add_argument(
|
||||
"-m",
|
||||
"--mode",
|
||||
choices=[mode.value for mode in ValidationMode],
|
||||
default='comment',
|
||||
help="Validation mode: 'comment' to only check if comments suggest changes, 'refinement' to check both comment suggestions and implementation. Default is 'comment'",
|
||||
type=ValidationMode,
|
||||
default=ValidationMode.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(
|
||||
"--check-diff-relevance",
|
||||
@ -249,6 +250,6 @@ if __name__ == "__main__":
|
||||
args.dataset,
|
||||
args.output,
|
||||
overwrite=args.overwrite,
|
||||
validation_mode=ValidationMode(args.mode),
|
||||
validation_mode=args.mode,
|
||||
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 typing import Optional
|
||||
from typing import Optional, Any, Sequence, Type, Union
|
||||
from github.Commit import Commit
|
||||
from github.PaginatedList import PaginatedList
|
||||
from github.PullRequestComment import PullRequestComment
|
||||
@ -182,3 +183,20 @@ def prompt_yes_no(prompt: str, *, default: Optional[bool] = None) -> bool:
|
||||
return default
|
||||
else:
|
||||
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