From c11189bab9a9e8e9fe9258cec8d1a8b8e2f97f18 Mon Sep 17 00:00:00 2001 From: nanguage Date: Sun, 28 Jan 2018 22:05:21 +0800 Subject: [PATCH] use parseopt2 replace docopt --- src/argparse.nim | 134 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.nim | 5 +- 2 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 src/argparse.nim diff --git a/src/argparse.nim b/src/argparse.nim new file mode 100644 index 0000000..6360127 --- /dev/null +++ b/src/argparse.nim @@ -0,0 +1,134 @@ +import tables +import parseopt2 + +type + + ValueKind* = enum + vkNone, + vkBool, + vkStr, + + Value* = object + case kind*: ValueKind + of vkNone: + nil + of vkBool: + bool_v: bool + of vkStr: + str_v: string + + +converter to_bool*(v: Value): bool = + case v.kind + of vkNone: + false + of vkBool: + v.bool_v + of vkStr: + v.str_v != nil and v.str_v.len > 0 + + +proc val(): Value = Value(kind: vkNone) +proc val(v: bool): Value = Value(kind: vkBool, bool_v: v) +proc val(v: string): Value = Value(kind: vkStr, str_v: v) + + +proc `$`*(v: Value): string = + case v.kind + of vkNone: + "nil" + of vkBool: + $v.bool_v + of vkStr: + v.str_v + + +proc parse_args*(doc: string): Table[string, Value] = + + result = { + "fq": val(), + "fa": val(), + "sam": val(), + "": val(), + "color-atla": val(), + "example-config": val(), + + "--phred": val(), + "--hist": val(), + "--delimiter": val(), + "--multiline": val(), + "--color": val(), + "--type": val(), + "--config-file": val(), + }.toTable() + + var arg_ct: int = 0 + + for kind, key, value in getopt(): + when not defined(release): + stderr.write(($kind) & " " & ($key) & " " & ($value) & "\n") + stderr.flushFile() + case kind + of cmdArgument: + case arg_ct: + of 0: + case key: + of "fq": + result["fq"] = val(true) + of "fa": + result["fa"] = val(true) + of "sam": + result["sam"] = val(true) + of "color-atla": + result["color-atla"] = val(true) + return result + of "example-config": + result["example-config"] = val(true) + return result + of 1: + result[""] = val(key) + else: + echo doc + quit(1) + inc arg_ct + of cmdShortOption: + case key + of "h": + echo doc + quit(0) + if key == "" and value == "": + result[""] = val("-") + inc arg_ct + of cmdLongOption: + case key: + of "h", "help": + echo doc + quit(0) + of "phred": + result["--phred"] = val(value) + of "hist": + result["--hist"] = val(value) + of "delimiter": + result["--delimiter"] = val(value) + of "multiline": + if value != "": + result["--multiline"] = val(value) + else: + result["--multiline"] = val("yes") + of "color": + result["--color"] = val(value) + of "type": + result["--type"] = val(value) + of "config-file": + result["--config-file"] = val(value) + of cmdEnd: + discard + + if (not result["fq"]) and (not result["fa"]) and (not result["sam"]) and + (not result["example-config"]) and (not result["color-atla"]): + echo doc + quit(1) + + +when isMainModule: + discard \ No newline at end of file diff --git a/src/main.nim b/src/main.nim index 877faf1..adf8c3d 100644 --- a/src/main.nim +++ b/src/main.nim @@ -21,8 +21,9 @@ Options: """ import os +import tables -import docopt +import argparse import color_atla import fastq_utils @@ -30,7 +31,7 @@ import fasta_utils import sam_utils import configs -let args = docopt(doc, version = "BioView 0.0.0") +var args = parse_args(doc) if args["color-atla"]: print_color_atla()