From 79876e177bf4055467a3289fba8b3402495adacc Mon Sep 17 00:00:00 2001 From: mratsim Date: Wed, 14 Mar 2018 18:10:09 +0100 Subject: [PATCH] Add cligen for CLI parsing + PoC license inputs --- .gitignore.nim => .gitignore | 0 nimbusLaunch.nimble | 2 +- src/nimbusLaunch.nim | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) rename .gitignore.nim => .gitignore (100%) diff --git a/.gitignore.nim b/.gitignore similarity index 100% rename from .gitignore.nim rename to .gitignore diff --git a/nimbusLaunch.nimble b/nimbusLaunch.nimble index 334040c..e6820e2 100644 --- a/nimbusLaunch.nimble +++ b/nimbusLaunch.nimble @@ -7,7 +7,7 @@ srcDir = "src" ### Dependencies -requires "nim >= 0.18.0" +requires "nim >= 0.18.0", "https://github.com/c-blake/cligen#head" proc test(name: string, lang: string = "cpp") = if not dirExists "build": diff --git a/src/nimbusLaunch.nim b/src/nimbusLaunch.nim index 50132d7..4dcbb52 100644 --- a/src/nimbusLaunch.nim +++ b/src/nimbusLaunch.nim @@ -7,4 +7,37 @@ # # at your option. This file may not be copied, modified, or distributed except according to those terms. +import cligen, argcvt # argcvt is part of cligen +import strutils + +type + License = enum + MIT, Apache2, GPLv2, GPLv3 + + Licenses = set[License] + +# Add Licenses support to cligen +template argParse(dst: Licenses, key: string, val: string, help: string) = + let args = val.split(',') + dst = {} + for input_license in args: + var isValid: bool = false + for supported_license in low(License)..high(License): # Interesting read: "parseEnum is slow" https://forum.nim-lang.org/t/2949 + if cmpIgnoreStyle(input_license, $supported_license) == 0: + incl(dst, supported_license) + isValid = true + if not isValid: + argRet(1, "Wrong input license(s) for param \"$1\"\n$2" % + [key, help]) + +template argHelp(helpT: seq[array[0..3, string]], defVal: Licenses, + parNm: string, sh: string, parHelp: string) = + helpT.add([ keys(parNm, sh), "Licenses", $defVal, parHelp ]) + +proc nimbusLaunch(licenses: Licenses = {MIT, Apache2}): int = + echo licenses + echo licenses.card # length (cardinality) of the set + +when isMainModule: + dispatch nimbusLaunch