use the gpm
https://github.com/you-win/godot-package-manager
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | Main.gd | 49 | ||||
| -rw-r--r-- | README.md | 48 | ||||
| -rw-r--r-- | addons/gdcli/Arg.gd | 3 | ||||
| -rw-r--r-- | addons/gdcli/LICENSE | 21 | ||||
| -rw-r--r-- | addons/gdcli/Parser.gd | 15 | ||||
| -rw-r--r-- | addons/gdcli/README.md | 39 | ||||
| -rw-r--r-- | addons/gdcli/package.json | 24 |
8 files changed, 187 insertions, 33 deletions
@@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 bendn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. @@ -2,27 +2,28 @@ extends Node func _ready(): - var p = Parser.new() - 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" - } - ) - ) - print(p.parse_arguments()) - print(p.help()) + var p = Parser.new() # create the parser + p.add_argument(Arg.new({ # add an argument + triggers = ["-F", "--fly"], # triggers + n_args = 2, # number of arguments + help = "Fly places", # help message + default = ["sky", "ground"] # default args if called without arguments + })) + p.add_argument(Arg.new({ # arg n2 + triggers = ["--eat"], + n_args = "*", # any number of arguments + help = "eats all args you give it", + dest = "restaurant" # the variable it goes into (defaults to longest trigger) + })) + p.add_argument(Arg.new({ + triggers = ["-H", "-?"], + dest = "help", + help = "show this help message and exit", + action = "store_true" + })) + var args = p.parse_arguments() # Parse + if args.get("help", false): # Check if we want help + print(p.help()) # Show help + else: + print(args) # Just show args otherwise + get_tree().quit() @@ -1,3 +1,47 @@ -# godot cli parser +# godot cli options parser -waiting on [godot-proposals/4815](https://github.com/godotengine/godot-proposals/issues/4815) for the full experience. +[](https://www.npmjs.com/package/@bendn/gdcli) + +A utility for parsing command line arguments for godot. + +> **Warning** +> +> Waiting 4 [godot-proposals/4815](https://github.com/godotengine/godot-proposals/issues/4815) for the full experience. + +--- + +## Features + +- 0-\* Arguments +- Default values +- Help creation +- Any number of triggers + +### Usage example + +```gdscript +var p = Parser.new() # create the parser +p.add_argument(Arg.new({ # add an argument + triggers = ["-F", "--fly"], # triggers + n_args = 2, # number of arguments + help = "Fly places", # help message + default = ["sky", "ground"] # default args if called without arguments +})) +p.add_argument(Arg.new({ # arg n2 + triggers = ["--eat"], + n_args = "*", # any number of arguments + help = "eats all args you give it", + dest = "restaurant" # the variable it goes into (defaults to longest trigger) +})) +p.add_argument(Arg.new({ + triggers = ["-H", "-?"], + dest = "help", + help = "show this help message and exit", + action = "store_true" +})) +var args = p.parse_arguments() # Parse +if args.get("help", false): # Check if we want help + print(p.help()) # Show help +else: + print(args) # Just show args otherwise +``` diff --git a/addons/gdcli/Arg.gd b/addons/gdcli/Arg.gd index a243dd6..0913d8c 100644 --- a/addons/gdcli/Arg.gd +++ b/addons/gdcli/Arg.gd @@ -30,15 +30,16 @@ func _init( } ) -> 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", "") + 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()) - action = opt_get(options, "action", "store") func opt_get(opt: Dictionary, key: String, df): diff --git a/addons/gdcli/LICENSE b/addons/gdcli/LICENSE new file mode 100644 index 0000000..9a9763e --- /dev/null +++ b/addons/gdcli/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 bendn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/addons/gdcli/Parser.gd b/addons/gdcli/Parser.gd index 2dbd4e8..a75ad7e 100644 --- a/addons/gdcli/Parser.gd +++ b/addons/gdcli/Parser.gd @@ -19,7 +19,12 @@ func parse_arguments(): var res := __search_target_args(current) if res: # if we found a arg star = "" # reset the star - if res.action == "store": + if res.arg_type: + match res.arg_type: + "*": + star = res.dest + args[res.dest] = cmdline_args.slice(i + 1, i) + elif res.action == "store": var length = cmdline_args.slice(i + 1, i + res.n_args).size() - res.n_args if length != 0: if res.default: @@ -54,16 +59,14 @@ func parse_arguments(): args[res.dest].append(c) elif res.action == "store_true": args[res.dest] = true - else: - match res.arg_type: - "*": - star = res.dest - args[res.dest] = cmdline_args.slice(i + 1, i) else: if star: 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 diff --git a/addons/gdcli/README.md b/addons/gdcli/README.md new file mode 100644 index 0000000..f695d06 --- /dev/null +++ b/addons/gdcli/README.md @@ -0,0 +1,39 @@ +# godot cli parser + +A utility for parsing command line arguments for godot. + +## Features + +- 0-\* Arguments +- Default values +- Help creation +- Any number of triggers + +### Usage example + +```gdscript +var p = Parser.new() # create the parser +p.add_argument(Arg.new({ # add an argument + triggers = ["-F", "--fly"], # triggers + n_args = 2, # number of arguments + help = "Fly places", # help message + default = ["sky", "ground"] # default args if called without arguments +})) +p.add_argument(Arg.new({ # arg n2 + triggers = ["--eat"], + n_args = "*", # any number of arguments + help = "eats all args you give it", + dest = "restaurant" # the variable it goes into (defaults to longest trigger) +})) +p.add_argument(Arg.new({ + triggers = ["-H", "-?"], + dest = "help", + help = "show this help message and exit", + action = "store_true" +})) +var args = p.parse_arguments() # Parse +if args.get("help", false): # Check if we want help + print(p.help()) # Show help +else: + print(args) # Just show args otherwise +``` diff --git a/addons/gdcli/package.json b/addons/gdcli/package.json new file mode 100644 index 0000000..f20f26d --- /dev/null +++ b/addons/gdcli/package.json @@ -0,0 +1,24 @@ +{ + "name": "@bendn/gdcli", + "version": "1.2.5", + "description": "godot cli utility", + "main": "Parser.gd", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/bend-n/gdcli.git" + }, + "keywords": [ + "godot", + "godot-engine", + "cli" + ], + "author": "bendn", + "license": "MIT", + "bugs": { + "url": "https://github.com/bend-n/gdcli/issues" + }, + "homepage": "https://github.com/bend-n/gdcli#readme" +}
\ No newline at end of file |