| -rw-r--r-- | Main.gd | 21 | ||||
| -rw-r--r-- | addons/gdcli/Arg.gd | 21 | ||||
| -rw-r--r-- | addons/gdcli/Parser.gd | 48 |
3 files changed, 66 insertions, 24 deletions
@@ -3,9 +3,26 @@ extends Node func _ready(): var p = Parser.new() - p.add_argument(Arg.new({triggers = ["-fly", "--fley"], n_args = 2, help = "i eat donkeys"})) + p.add_argument( + Arg.new( + { + triggers = ["-fly", "--fley"], + n_args = 2, + help = "i eat donkeys", + default = ["donkey kong", "ha"] + } + ) + ) p.add_argument(Arg.new({triggers = ["-bard", "--flgersog"], n_args = 2, help = "me 4"})) p.add_argument(Arg.new({triggers = ["--radiation"], n_args = "*", help = "i am radiation"})) - p.add_argument(Arg.new({triggers=["-h", "--help", "-?"], help="show this help message and exit", action="store_true", dest="help"})) + p.add_argument( + Arg.new( + { + triggers = ["-h", "--help", "-?"], + help = "show this help message and exit", + action = "store_true" + } + ) + ) print(p.parse_arguments()) print(p.help()) diff --git a/addons/gdcli/Arg.gd b/addons/gdcli/Arg.gd index 9d43135..a243dd6 100644 --- a/addons/gdcli/Arg.gd +++ b/addons/gdcli/Arg.gd @@ -3,7 +3,7 @@ class_name Arg var n_args := 0 var arg_type := "" # can be * -var triggers: PoolStringArray = [] +var triggers := [] var help := "" var arg_names := "" var action := "store" @@ -24,8 +24,8 @@ func _init( n_args = 0, help = "", dest = "", - default = "", - arg_names = "", + default = "", # the default when (no/not enough) value(s) are given + arg_names = "", # names of the arguments, in string format, used in the help message action = "store" } ) -> void: @@ -45,10 +45,6 @@ func opt_get(opt: Dictionary, key: String, df): return opt[key] if key in opt else df -func add_trigger(t: String) -> void: - triggers.append(t) - - func get_arg_names(): var r_names := "" if not arg_type: @@ -60,10 +56,11 @@ func get_arg_names(): func small_arg() -> String: - var small := "" - for t in triggers: - small = t if len(t) < len(small) or !small else small - return small + return triggers.min() + + +func long_arg() -> String: + return triggers.max() func dest_name() -> String: @@ -71,4 +68,4 @@ func dest_name() -> String: func format_triggers() -> String: - return "%s %s" % [triggers.join(", "), "[%s]" % arg_names if arg_names else ""] + return "%s %s" % [PoolStringArray(triggers).join(", "), "[%s]" % arg_names if arg_names else ""] diff --git a/addons/gdcli/Parser.gd b/addons/gdcli/Parser.gd index ab8074b..2dbd4e8 100644 --- a/addons/gdcli/Parser.gd +++ b/addons/gdcli/Parser.gd @@ -13,14 +13,45 @@ func parse_arguments(): var args := {unhandled = []} var i := -1 var star: String - while i < len(cmdline_args) - 1: + while i < len(cmdline_args) - 1: # go through the cmdline args i += 1 - var res := __search_target_args(cmdline_args[i]) - if res: - star = "" + var current = cmdline_args[i] + var res := __search_target_args(current) + if res: # if we found a arg + star = "" # reset the star if res.action == "store": - args[res.dest] = cmdline_args.slice(i + 1, i + res.n_args) - i += res.n_args + var length = cmdline_args.slice(i + 1, i + res.n_args).size() - res.n_args + if length != 0: + if res.default: + args[res.dest] = res.default + else: + var errstr = "Missing %s argument%s for %s" + errstr = errstr % [length * -1, "s" if length < -1 else "", current] + push_error(errstr) + return null + for c in cmdline_args.slice(i + 1, i + res.n_args): # search the next n_args args + i += 1 + if __search_target_args(c): # if it is a argument + if !res.default: # and there is no default + var errstr = "Missing %s argument%s for %s" + var lent = ( + len(args[res.dest]) - res.n_args + if res.dest in args + else -res.n_args + ) + errstr = errstr % [lent * -1, "s" if lent < -1 else "", current] + push_error(errstr) + return null # and return + else: + i -= 1 + args[res.dest] = res.default # if there is a default, set it to that + break + if res.n_args == 1: + args[res.dest] = c # if there is only one argument, set it to that + elif not res.dest in args: + args[res.dest] = [c] # otherwise put it in a array + else: + args[res.dest].append(c) elif res.action == "store_true": args[res.dest] = true else: @@ -33,9 +64,6 @@ func parse_arguments(): args[star].append(cmdline_args[i]) else: args.unhandled.append(cmdline_args[i]) - for r in target_args: - if not r.dest in args: - args[r.dest] = r.default return args @@ -85,5 +113,5 @@ static func exec_ext() -> String: elif OS.has_feature("X11"): return ".x86_64" elif OS.has_feature("web"): - return ".html" # how do you ever manage to get here tho + return ".html" # how do you ever manage to get here tho return "" |