store_true mode
| -rw-r--r-- | Main.gd | 1 | ||||
| -rw-r--r-- | addons/gdcli/Arg.gd | 22 | ||||
| -rw-r--r-- | addons/gdcli/Parser.gd | 37 | ||||
| -rw-r--r-- | project.godot | 4 |
4 files changed, 46 insertions, 18 deletions
@@ -6,5 +6,6 @@ func _ready(): p.add_argument(Arg.new({triggers = ["-fly", "--fley"], n_args = 2, help = "i eat donkeys"})) 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"})) print(p.parse_arguments()) print(p.help()) diff --git a/addons/gdcli/Arg.gd b/addons/gdcli/Arg.gd index 828bbf1..9d43135 100644 --- a/addons/gdcli/Arg.gd +++ b/addons/gdcli/Arg.gd @@ -6,6 +6,7 @@ var arg_type := "" # can be * var triggers: PoolStringArray = [] var help := "" var arg_names := "" +var action := "store" var dest := "" var default = "" @@ -18,7 +19,15 @@ func find_trigger(to_find: String) -> String: func _init( - options := {triggers = [], n_args = 0, help = "", dest = "", default = "", arg_names = ""} + options := { + triggers = [], + n_args = 0, + help = "", + dest = "", + default = "", + arg_names = "", + action = "store" + } ) -> void: help = opt_get(options, "help", "") triggers = opt_get(options, "triggers", []) @@ -28,7 +37,8 @@ func _init( n_args = opt_get(options, "n_args", 0) dest = opt_get(options, "dest", dest_name()) default = opt_get(options, "default", "") - arg_names = opt_get(options, "arg_namaes", get_arg_names()) + arg_names = opt_get(options, "arg_names", get_arg_names()) + action = opt_get(options, "action", "store") func opt_get(opt: Dictionary, key: String, df): @@ -40,13 +50,13 @@ func add_trigger(t: String) -> void: func get_arg_names(): - var r_names := "[" + var r_names := "" if not arg_type: for i in n_args: r_names += " arg%s " % i else: - r_names += " ..." - return r_names + "]" + r_names += "..." + return r_names func small_arg() -> String: @@ -61,4 +71,4 @@ func dest_name() -> String: func format_triggers() -> String: - return triggers.join(", ") + " " + arg_names + return "%s %s" % [triggers.join(", "), "[%s]" % arg_names if arg_names else ""] diff --git a/addons/gdcli/Parser.gd b/addons/gdcli/Parser.gd index d618a0f..ab8074b 100644 --- a/addons/gdcli/Parser.gd +++ b/addons/gdcli/Parser.gd @@ -17,10 +17,12 @@ func parse_arguments(): i += 1 var res := __search_target_args(cmdline_args[i]) if res: - if res.n_args: + star = "" + if res.action == "store": args[res.dest] = cmdline_args.slice(i + 1, i + res.n_args) i += res.n_args - star = "" + elif res.action == "store_true": + args[res.dest] = true else: match res.arg_type: "*": @@ -29,7 +31,6 @@ func parse_arguments(): else: if star: args[star].append(cmdline_args[i]) - breakpoint else: args.unhandled.append(cmdline_args[i]) for r in target_args: @@ -45,28 +46,44 @@ func __search_target_args(to_find: String) -> Arg: return null -func help() -> String: +func help(description := "") -> String: var size = 1 for arg in target_args: # find the longest arguments size = len(arg.format_triggers()) if len(arg.format_triggers()) > size else size var options := "" - var usage := "godot " + var usage := ( + "%s%s [options...]" + % [ProjectSettings.get_setting("application/config/name"), exec_ext()] + ) for arg in target_args: - usage += "[%s] " % arg.small_arg() options += arg.format_triggers() + " " for _i in range(size - len(arg.format_triggers())): - options += " " + options += " " # add spaces to align the options options += arg.help + "\n" var help = """ usage: {usage} - {description} - options: {options} """.format( - {usage = usage, description = "", options = options.indent(" ")} + { + usage = usage, + description = "\n%s\n" % description if description else "", + options = options.indent(" ") + } ) return help + + +static func exec_ext() -> String: + if OS.has_feature("Windows"): + return ".exe" + elif OS.has_feature("OSX"): + return "" + 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 "" diff --git a/project.godot b/project.godot index 4f9b632..709b4bf 100644 --- a/project.godot +++ b/project.godot @@ -12,12 +12,12 @@ _global_script_classes=[ { "base": "Reference", "class": "Arg", "language": "GDScript", -"path": "res://addons/Arg.gd" +"path": "res://addons/gdcli/Arg.gd" }, { "base": "Reference", "class": "Parser", "language": "GDScript", -"path": "res://addons/Parser.gd" +"path": "res://addons/gdcli/Parser.gd" } ] _global_script_class_icons={ "Arg": "", |