diff options
author | kitsunyan | 2018-04-29 13:44:59 +0000 |
---|---|---|
committer | kitsunyan | 2018-04-29 13:44:59 +0000 |
commit | 9525d77603309b81faa43b14137b0a48f7391850 (patch) | |
tree | 3d8d2ec858681606c2ec5b5228529208dda91039 /src | |
parent | 34fb7795b12ad3aade4905438f44c9be95d9d501 (diff) |
Simplify arguments checking
Diffstat (limited to 'src')
-rw-r--r-- | src/common.nim | 4 | ||||
-rw-r--r-- | src/feature/localquery.nim | 4 | ||||
-rw-r--r-- | src/feature/syncinstall.nim | 23 | ||||
-rw-r--r-- | src/feature/syncsearch.nim | 2 | ||||
-rw-r--r-- | src/main.nim | 48 | ||||
-rw-r--r-- | src/pacman.nim | 27 |
6 files changed, 55 insertions, 53 deletions
diff --git a/src/common.nim b/src/common.nim index 92c850d..b66237a 100644 --- a/src/common.nim +++ b/src/common.nim @@ -38,7 +38,7 @@ type ] proc checkAndRefresh*(color: bool, args: seq[Argument]): tuple[code: int, args: seq[Argument]] = - let refreshCount = args.count((some("y"), "refresh")) + let refreshCount = args.count(%%%"refresh") if refreshCount > 0: let code = pacmanRun(true, color, args .keepOnlyOptions(commonOptions, upgradeCommonOptions) & @@ -46,7 +46,7 @@ proc checkAndRefresh*(color: bool, args: seq[Argument]): tuple[code: int, args: ("y", none(string), ArgumentType.short).repeat(refreshCount)) let callArgs = args - .filter(arg => not arg.matchOption((some("y"), "refresh"))) + .filter(arg => not arg.matchOption(%%%"refresh")) (code, callArgs) else: (0, args) diff --git a/src/feature/localquery.nim b/src/feature/localquery.nim index f4142ee..ccc1311 100644 --- a/src/feature/localquery.nim +++ b/src/feature/localquery.nim @@ -20,8 +20,8 @@ proc handleQueryOrphans*(args: seq[Argument], config: Config): int = if results.len > 0: let newArgs = args.filter(arg => not arg.isTarget and - not arg.matchOption((some("t"), "unrequired")) and - not arg.matchOption((some("d"), "deps"))) & + not arg.matchOption(%%%"unrequired") and + not arg.matchOption(%%%"deps")) & results.map(r => (r, none(string), ArgumentType.target)) pacmanExec(false, config.color, newArgs) elif targets.len == 0: diff --git a/src/feature/syncinstall.nim b/src/feature/syncinstall.nim index 81177ea..c214198 100644 --- a/src/feature/syncinstall.nim +++ b/src/feature/syncinstall.nim @@ -1242,22 +1242,21 @@ proc resolveBuildTargets(config: Config, targets: seq[PackageTarget], proc handleSyncInstall*(args: seq[Argument], config: Config): int = let (_, callArgs) = checkAndRefresh(config.color, args) - let upgradeCount = args.count((some("u"), "sysupgrade")) - let needed = args.check((none(string), "needed")) - let noaur = args.check((none(string), "noaur")) - let build = args.check((none(string), "build")) - - let printModeArg = args.check((some("p"), "print")) - let printModeFormat = args.filter(arg => arg - .matchOption((none(string), "print-format"))).optLast + let upgradeCount = args.count(%%%"sysupgrade") + let needed = args.check(%%%"needed") + let noaur = args.check(%%%"noaur") + let build = args.check(%%%"build") + + let printModeArg = args.check(%%%"print") + let printModeFormat = args.filter(arg => arg.matchOption(%%%"print-format")).optLast let printFormat = if printModeArg or printModeFormat.isSome: some(printModeFormat.map(arg => arg.value.get).get("%l")) else: none(string) let noconfirm = args - .filter(arg => arg.matchOption((none(string), "confirm")) or - arg.matchOption((none(string), "noconfirm"))).optLast + .filter(arg => arg.matchOption(%%%"confirm") or + arg.matchOption(%%%"noconfirm")).optLast .map(arg => arg.key == "noconfirm").get(false) let targets = args.packageTargets @@ -1281,9 +1280,9 @@ proc handleSyncInstall*(args: seq[Argument], config: Config): int = .filter(i => not i.explicit).map(i => i.name).toSet let keepNames = foreignExplicitsNamesSet + foreignDepsNamesSet + targetNamesSet - let explicits = if args.check((none(string), "asexplicit")): + let explicits = if args.check(%%%"asexplicit"): targetNamesSet + foreignExplicitsNamesSet + foreignDepsNamesSet - elif args.check((none(string), "asdeps")): + elif args.check(%%%"asdeps"): initSet[string]() else: foreignExplicitsNamesSet + (targetNamesSet - foreignDepsNamesSet) diff --git a/src/feature/syncsearch.nim b/src/feature/syncsearch.nim index 7b2e77c..178a839 100644 --- a/src/feature/syncsearch.nim +++ b/src/feature/syncsearch.nim @@ -7,7 +7,7 @@ import proc handleSyncSearch*(args: seq[Argument], config: Config): int = let (_, callArgs) = checkAndRefresh(config.color, args) - let quiet = args.check((some("q"), "quiet")) + let quiet = args.check(%%%"quiet") let (aurPackages, aerrors) = findAurPackages(args.targets) for e in aerrors: printError(config.color, e) diff --git a/src/main.nim b/src/main.nim index b259989..d156834 100644 --- a/src/main.nim +++ b/src/main.nim @@ -32,7 +32,7 @@ proc passValidation(args: seq[Argument], config: Config, proc handleDatabase(args: seq[Argument], config: Config): int = let nonRootArgs = [ - (some("k"), "check") + %%%"check" ] passValidation(args, config, nonRootArgs, [], @@ -40,7 +40,7 @@ proc handleDatabase(args: seq[Argument], config: Config): int = proc handleFiles(args: seq[Argument], config: Config): int = let rootArgs = [ - (some("y"), "refresh") + %%%"refresh" ] passValidation(args, config, [], rootArgs, @@ -50,9 +50,9 @@ proc handleQuery(args: seq[Argument], config: Config): int = let queryArgs = args.removeMatchOptions(commonOptions) if queryArgs.checkOpGroup(OpGroup.localQuery) and - not queryArgs.check((some("e"), "explicit")) and - queryArgs.check((some("d"), "deps")) and - queryArgs.count((some("t"), "unrequired")) >= 3: + not queryArgs.check(%%%"explicit") and + queryArgs.check(%%%"deps") and + queryArgs.count(%%%"unrequired") >= 3: handleQueryOrphans(args, config) else: passValidation(args, config, [], [], @@ -60,8 +60,8 @@ proc handleQuery(args: seq[Argument], config: Config): int = proc handleRemove(args: seq[Argument], config: Config): int = let nonRootArgs = [ - (some("p"), "print"), - (none(string), "print-format") + %%%"print", + %%%"print-format" ] passValidation(args, config, nonRootArgs, [], @@ -76,15 +76,15 @@ proc handleSync(args: seq[Argument], config: Config): int = printError(config.color, trp("invalid option: '%s' and '%s' may not be used together\n") % ["--" & left, "--" & right]) 1 - elif syncArgs.check((some("i"), "info")) and + elif syncArgs.check(%%%"info") and syncArgs.checkOpGroup(OpGroup.syncQuery): handleSyncInfo(args, config) - elif syncArgs.check((some("s"), "search")) and + elif syncArgs.check(%%%"search") and syncArgs.checkOpGroup(OpGroup.syncSearch): handleSyncSearch(args, config) elif syncArgs.checkOpGroup(OpGroup.syncInstall) and - (args.check((some("u"), "sysupgrade")) or args.targets.len > 0): - let printMode = args.check((some("p"), "print")) or args.check((none(string), "print-format")) + (args.check(%%%"sysupgrade") or args.targets.len > 0): + let printMode = args.check(%%%"print") or args.check(%%%"print-format") if currentUser.uid != 0 and config.sudoExec and not printMode: let collectedArgs = @[sudoCmd, getAppFilename()] & @@ -92,11 +92,11 @@ proc handleSync(args: seq[Argument], config: Config): int = execResult(collectedArgs) else: let isNonDefaultRoot = not config.isRootDefault - let isSkipDeps = args.check((some("d"), "nodeps")) + let isSkipDeps = args.check(%%%"nodeps") let isRootNoDrop = currentUser.uid == 0 and not canDropPrivileges() - let build = args.check((none(string), "build")) - let noaur = args.check((none(string), "noaur")) + let build = args.check(%%%"build") + let noaur = args.check(%%%"noaur") let noBuild = isNonDefaultRoot or isSkipDeps or isRootNoDrop @@ -131,16 +131,16 @@ proc handleSync(args: seq[Argument], config: Config): int = handleSyncInstall(args, config) else: let nonRootArgs = [ - (some("p"), "print"), - (none(string), "print-format"), - (some("g"), "groups"), - (some("i"), "info"), - (some("l"), "list"), - (some("s"), "search") + %%%"print", + %%%"print-format", + %%%"groups", + %%%"info", + %%%"list", + %%%"search" ] let rootArgs = [ - (some("y"), "refresh"), + %%%"refresh" ] passValidation(args, config, nonRootArgs, rootArgs, @@ -151,8 +151,8 @@ proc handleDeptest(args: seq[Argument], config: Config): int = proc handleUpgrade(args: seq[Argument], config: Config): int = let nonRootArgs = [ - (some("p"), "print"), - (none(string), "print-format") + %%%"print", + %%%"print-format" ] passValidation(args, config, nonRootArgs, [], @@ -225,7 +225,7 @@ proc run(parsedArgs: seq[Argument], config: Config): withErrorHandler(some(config.color), int): let operation = getOperation(parsedArgs) if operation != OperationType.invalid and - parsedArgs.check((some("h"), "help")): + parsedArgs.check(%%%"help"): handleHelp(operation) 0 elif operation != OperationType.invalid and diff --git a/src/pacman.nim b/src/pacman.nim index 58bf42b..2ad3560 100644 --- a/src/pacman.nim +++ b/src/pacman.nim @@ -239,6 +239,9 @@ proc checkOpGroup*(args: seq[Argument], group: OpGroup): bool = args.whitelisted(toCheck) +proc `%%%`*(long: string): OptionPair = + allOptions.filter(o => o.pair.long == long)[0].pair + proc filterExtensions*(args: seq[Argument], removeMatches: bool, keepTargets: bool): seq[Argument] = let argsSeq = lc[x.pair | (x <- allOptions, x.extension), OptionPair] @@ -283,7 +286,7 @@ proc pacmanExec*(root: bool, color: bool, args: varargs[Argument]): int = let colorStr = if color: "always" else: "never" let argsSeq = ("color", some(colorStr), ArgumentType.long) & - @args.filter(arg => not arg.matchOption((none(string), "color"))) + @args.filter(arg => not arg.matchOption(%%%"color")) let collectedArgs = lc[x | (y <- argsSeq, x <- y.collectArg), string] pacmanExec(useRoot, collectedArgs) @@ -327,7 +330,7 @@ proc obtainPacmanConfig*(args: seq[Argument]): PacmanConfig = proc getAll(pair: OptionPair): seq[string] = args.filter(arg => arg.matchOption(pair)).map(arg => arg.value.get) - let configFile = getAll((none(string), "config")).optLast.get(sysConfDir & "/pacman.conf") + let configFile = getAll(%%%"config").optLast.get(sysConfDir & "/pacman.conf") let (configTable, wasError) = readConfigFile(configFile) let options = configTable.opt("options").map(t => t[]).get(initTable[string, string]()) @@ -341,18 +344,18 @@ proc obtainPacmanConfig*(args: seq[Argument]): PacmanConfig = let colors = toSeq(enumerate[ColorMode]()) colors.filter(c => $c == color).optLast.get(ColorMode.colorNever) - let root = getAll((some("r"), "root")).optLast.orElse(defaultConfig.rootOption) - let db = getAll((some("b"), "dbpath")).optLast.orElse(defaultConfig.dbOption) - let gpg = getAll((none(string), "gpgdir")).optLast.orElse(defaultConfig.gpgOption) - let arch = getAll((none(string), "arch")).optLast.get(defaultConfig.arch) - let colorStr = getAll((none(string), "color")).optLast.get($defaultConfig.colorMode) + let root = getAll(%%%"root").optLast.orElse(defaultConfig.rootOption) + let db = getAll(%%%"dbpath").optLast.orElse(defaultConfig.dbOption) + let gpg = getAll(%%%"gpgdir").optLast.orElse(defaultConfig.gpgOption) + let arch = getAll(%%%"arch").optLast.get(defaultConfig.arch) + let colorStr = getAll(%%%"color").optLast.get($defaultConfig.colorMode) let color = getColor(colorStr) - let debug = args.check((none(string), "debug")) - let progressBar = not args.check((none(string), "noprogressbar")) - let ignorePkgs = lc[x | (y <- getAll((none(string), "ignore")), + let debug = args.check(%%%"debug") + let progressBar = not args.check(%%%"noprogressbar") + let ignorePkgs = lc[x | (y <- getAll(%%%"ignore"), x <- y.split(',')), string].toSet - let ignoreGroups = lc[x | (y <- getAll((none(string), "ignoregroups")), + let ignoreGroups = lc[x | (y <- getAll(%%%"ignoregroup"), x <- y.split(',')), string].toSet let hasKeyserver = forkWaitRedirect(() => (block: @@ -367,7 +370,7 @@ proc obtainPacmanConfig*(args: seq[Argument]): PacmanConfig = let pgpKeyserver = if hasKeyserver: none(string) else: (block: - let argPgpKeyserver = getAll((none(string), "keyserver")).optLast + let argPgpKeyserver = getAll(%%%"keyserver").optLast if argPgpKeyserver.isSome: argPgpKeyserver else: |