Refactoriza el siguiente código para hacerlo más eficiente y fácil de entender. [code]
- User
Refactoriza el siguiente código para hacerlo más eficiente y fácil de entender.
import std/[os, tables, strformat, strutils] import pkg/tinyre import types, utils, data_validator type SeqOrBool = object seqVal*: seq[string] boolVal*: bool Error = object there*: bool msg*: string CmdosData* = Table[string, SeqOrBool] proc parseRange(s: string): (int, int) = var parts = s.split(re"\.\.") if parts.len != 2: raise newException(ValueError, &"[CmdosArg.nVal] Invalid range format: '{s}'") let start = parts[0].parseInt let finish = parts[1].parseInt return (start, finish) proc hasError(status: bool, text: string = ""): Error = result = Error(there: status, msg: text) ### Process input options proc processCmd*(command: static CmdosCmd, ignoreFirstOpt: bool = false, inputs: seq[string] = os.commandLineParams()): (Table[string, SeqOrBool], Error) = # Validate the commands const status = isValidCmdosCmd(command) let reExclude = tinyre.re"^-" var initialValues: seq[string] var data = initTable[string, SeqOrBool]() var err = hasError(false) var ignoreFirst = ignoreFirstOpt # If `ignoreFirst` is true, capture all initial arguments. # if ignoreFirst and inputs.len > 0: # for i in 0..<inputs.len: # if tinyre.contains(inputs[i], reExclude): break # initialValues.add(inputs[i]) # El comando existe? if inputs[0] notin command.alias: err = hasError(true, &"{inputs[0]} is not a command.") return (data, err) for arg in command.args: let longAlias = getLongestWord(arg.alias) let rangeString = arg.nVal var values: seq[string] var haveValue = false # Iterate over the input arguments for i in 0..<inputs.len: if inputs[i] in arg.alias: var j, start, finish: int var insufficient = false var limitedCase: string j = i + 1 # Determine the range of values if rangeString == "+": finish = inputs.len elif rangeString.contains(".."): let (inicio, fin) = parseRange(rangeString) finish = inicio limitedCase = ".." else: finish = parseInt(rangeString) limitedCase = "n" # Check if 'finish' is greater than the length of inputs if finish > inputs.len: err = hasError(true, "Invalid range value.") return (data, err) # Handle flag if arg.nVal == "0": data[longAlias] = SeqOrBool(seqVal: @[""], boolVal: true) haveValue = true else: var n = 0 haveValue = true # Handle option with inputs while j < inputs.len and not tinyre.contains(inputs[j], reExclude): echo "$1 $2" % [$j, $n] case limitedCase: of "n": if (n == finish): insufficient = false echo "Hi!" break of "..": if (n >= start and n <= finish): insufficient = false break insufficient = true values.add(inputs[j]) n += 1 inc(j) if insufficient == true: err = hasError(true, "Insufficient arguments") return (data, err) break # Handle default values and initial values if longAlias == "" and initialValues.len > 0 and ignoreFirst: data["--"] = SeqOrBool(seqVal: initialValues) ignoreFirst = false continue if longAlias == "" and initialValues.len == 0 and ignoreFirst: err = hasError(true, "Insufficient arguments to run the command.") return (data, err) if arg.nVal == "0" and not haveValue: data[longAlias] = SeqOrBool(seqVal: @[""], boolVal: false) if values.len == 0 and arg.defVal.len > 0: data[longAlias] = SeqOrBool(seqVal: arg.defVal) if values.len > 0: data[longAlias] = SeqOrBool(seqVal: values) return (data, err) - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.