| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Main.gd | 4 | ||||
| -rw-r--r-- | Main.tscn | 6 | ||||
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | addons/gdcli/Arg.gd | 44 | ||||
| -rw-r--r-- | addons/gdcli/Parser.gd | 42 | ||||
| -rw-r--r-- | addons/gdcli/README.md | 15 | ||||
| -rw-r--r-- | addons/gdcli/package.json | 4 | ||||
| -rw-r--r-- | project.godot | 7 |
9 files changed, 75 insertions, 54 deletions
@@ -1,2 +1,3 @@ .vscode/ .import/ +.godot/ @@ -16,12 +16,12 @@ func _ready(): dest = "restaurant" # the variable it goes into (defaults to longest trigger) })) p.add_argument(Arg.new({ - triggers = ["-H", "-?"], + triggers = ["-H", "-?", "--help"], dest = "help", help = "show this help message and exit", action = "store_true" })) - var args = p.parse_arguments() # Parse + var args = p.parse_arguments(OS.get_cmdline_args() + OS.get_cmdline_user_args()) # Parse if args.get("help", false): # Check if we want help print(p.help()) # Show help else: @@ -1,6 +1,6 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=2 format=3 uid="uid://bby77ajiap00o"] -[ext_resource path="res://Main.gd" type="Script" id=1] +[ext_resource type="Script" path="res://Main.gd" id="1_vky5t"] [node name="Main" type="Node2D"] -script = ExtResource( 1 ) +script = ExtResource("1_vky5t") @@ -1,11 +1,13 @@ # godot cli options parser -[](https://godotengine.org "Made with godot") +[](https://godotengine.org "Made with godot") [](https://www.npmjs.com/package/@bendn/gdcli) <a href='https://ko-fi.com/bendn' title='Buy me a coffee' target='_blank'><img height='28' src='https://storage.ko-fi.com/cdn/brandasset/kofi_button_red.png' alt='Buy me a coffee'> </a> A utility for parsing command line arguments for godot. +> **Note** Versions < 2.0.0 are for 3.x + > **Warning** > > Waiting 4 [godot-proposals/4815](https://github.com/godotengine/godot-proposals/issues/4815) for the full experience. @@ -41,7 +43,7 @@ p.add_argument(Arg.new({ help = "show this help message and exit", action = "store_true" })) -var args = p.parse_arguments() # Parse +var args = p.parse_arguments(OS.get_cmdline_args() + OS.get_cmdline_user_args()) # Parse if args.get("help", false): # Check if we want help print(p.help()) # Show help else: diff --git a/addons/gdcli/Arg.gd b/addons/gdcli/Arg.gd index 0913d8c..f0b280a 100644 --- a/addons/gdcli/Arg.gd +++ b/addons/gdcli/Arg.gd @@ -1,4 +1,4 @@ -extends Reference +extends RefCounted class_name Arg var n_args := 0 @@ -23,50 +23,50 @@ func _init( triggers = [], n_args = 0, help = "", - dest = "", + dest = "", # defaults to longest trigger 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: - help = opt_get(options, "help", "") - action = opt_get(options, "action", "store") - triggers = opt_get(options, "triggers", []) - if typeof(opt_get(options, "n_args", 0)) == TYPE_STRING: - arg_type = opt_get(options, "n_args", "") + help = options.get("help", "") + action = options.get("action", "store") + triggers = options.get("triggers", []) + triggers.sort_custom(func(a: String, b: String): return len(a) < len(b)) + if options.get("n_args", 0) is String: + arg_type = options.get("n_args", "") action = "" else: - 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_names", get_arg_names()) + n_args = options.get("n_args", 0) + dest = options.get("dest", dest_name()) + default = options.get("default", "") + arg_names = options.get("arg_names", get_arg_names()) -func opt_get(opt: Dictionary, key: String, df): - return opt[key] if key in opt else df +func opt_get(opt: Dictionary, key: String, df: Variant): + return opt.get(key, df) func get_arg_names(): var r_names := "" - if not arg_type: + if arg_type.is_empty(): for i in n_args: r_names += " arg%s " % i else: r_names += "..." return r_names - func small_arg() -> String: - return triggers.min() - + return triggers[0] if triggers.size() > 0 else "" func long_arg() -> String: - return triggers.max() - + return triggers[-1] if triggers.size() > 0 else "" func dest_name() -> String: - return small_arg().replace("-", "") - + return long_arg().replace("-", "") func format_triggers() -> String: - return "%s %s" % [PoolStringArray(triggers).join(", "), "[%s]" % arg_names if arg_names else ""] + return "%s %s" % [", ".join(triggers), "[%s]" % arg_names if arg_names else ""] + +func _to_string() -> String: + return "Arg<%s(%s%s)>" % [long_arg().replace("-", ""), action, arg_type] diff --git a/addons/gdcli/Parser.gd b/addons/gdcli/Parser.gd index a75ad7e..7bd5743 100644 --- a/addons/gdcli/Parser.gd +++ b/addons/gdcli/Parser.gd @@ -1,4 +1,4 @@ -extends Reference +extends RefCounted class_name Parser var target_args := [] @@ -7,39 +7,47 @@ var target_args := [] func add_argument(arg: Arg): target_args.append(arg) - -func parse_arguments(): - var cmdline_args = Array(OS.get_cmdline_args()) - var args := {unhandled = []} +func parse_arguments(cmdline_args: PackedStringArray): + var args := {unhandled = PackedStringArray([])} var i := -1 var star: String while i < len(cmdline_args) - 1: # go through the cmdline args i += 1 var current = cmdline_args[i] +# print("searching ", current) var res := __search_target_args(current) if res: # if we found a arg +# print("found arg ", res) star = "" # reset the star if res.arg_type: match res.arg_type: "*": star = res.dest - args[res.dest] = cmdline_args.slice(i + 1, i) + args[res.dest] = PackedStringArray() +# print("adding %s to star" % cmdline_args[i+1]) elif res.action == "store": - var length = cmdline_args.slice(i + 1, i + res.n_args).size() - res.n_args - if length != 0: +# print("fixed arg") + var length := cmdline_args.size()-i-1-res.n_args + if length < 0: +# print("no args supplied") if res.default: args[res.dest] = res.default +# print("set default") + continue 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 + for c in cmdline_args.slice(i + 1, i + 1 + res.n_args): # search the next n_args args +# print("adding args") +# print("searching(nargs) ", c) 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 = ( +# print("found arg") + if res.default.is_empty(): # 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 @@ -48,13 +56,14 @@ func parse_arguments(): push_error(errstr) return null # and return else: +# print("set def") 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 + 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 + args[res.dest] = PackedStringArray([c]) # otherwise put it in a array else: args[res.dest].append(c) elif res.action == "store_true": @@ -64,9 +73,6 @@ func parse_arguments(): args[star].append(cmdline_args[i]) else: args.unhandled.append(cmdline_args[i]) - for k in args: - if typeof(args[k]) == TYPE_ARRAY: - args[k] = PoolStringArray(args[k]) return args @@ -96,7 +102,7 @@ func help(description := "") -> String: var help = """ usage: {usage} {description} -options: +options: {options} """.format( { diff --git a/addons/gdcli/README.md b/addons/gdcli/README.md index f695d06..b2198ee 100644 --- a/addons/gdcli/README.md +++ b/addons/gdcli/README.md @@ -1,7 +1,18 @@ -# godot cli parser +# godot cli options parser + +[](https://godotengine.org "Made with godot") +<a href='https://ko-fi.com/bendn' title='Buy me a coffee' target='_blank'><img height='28' src='https://storage.ko-fi.com/cdn/brandasset/kofi_button_red.png' alt='Buy me a coffee'> </a> A utility for parsing command line arguments for godot. +> **Note** Versions < 2.0.0 are for 3.x + +> **Warning** +> +> Waiting 4 [godot-proposals/4815](https://github.com/godotengine/godot-proposals/issues/4815) for the full experience. + +--- + ## Features - 0-\* Arguments @@ -31,7 +42,7 @@ p.add_argument(Arg.new({ help = "show this help message and exit", action = "store_true" })) -var args = p.parse_arguments() # Parse +var args = p.parse_arguments(OS.get_cmdline_args() + OS.get_cmdline_user_args()) # Parse if args.get("help", false): # Check if we want help print(p.help()) # Show help else: diff --git a/addons/gdcli/package.json b/addons/gdcli/package.json index f20f26d..2f7128c 100644 --- a/addons/gdcli/package.json +++ b/addons/gdcli/package.json @@ -1,6 +1,6 @@ { "name": "@bendn/gdcli", - "version": "1.2.5", + "version": "2.0.2", "description": "godot cli utility", "main": "Parser.gd", "scripts": { @@ -21,4 +21,4 @@ "url": "https://github.com/bend-n/gdcli/issues" }, "homepage": "https://github.com/bend-n/gdcli#readme" -}
\ No newline at end of file +} diff --git a/project.godot b/project.godot index 709b4bf..df8d769 100644 --- a/project.godot +++ b/project.godot @@ -6,9 +6,9 @@ ; [section] ; section goes between [] ; param=value ; assign values to parameters -config_version=4 +config_version=5 -_global_script_classes=[ { +_global_script_classes=[{ "base": "Reference", "class": "Arg", "language": "GDScript", @@ -18,7 +18,7 @@ _global_script_classes=[ { "class": "Parser", "language": "GDScript", "path": "res://addons/gdcli/Parser.gd" -} ] +}] _global_script_class_icons={ "Arg": "", "Parser": "" @@ -28,6 +28,7 @@ _global_script_class_icons={ config/name="gdcli" run/main_scene="res://Main.tscn" +config/features=PackedStringArray("4.1") [editor] |