Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5128851965 | ||
|
|
28329fa0b7 | ||
|
|
c675f6b659 | ||
|
|
49eaaeabf5 |
@@ -28,9 +28,9 @@ And on the command line, you pass flags like so:
|
||||
./<program name> -isNumber=45 -isRunning=false ......
|
||||
```
|
||||
|
||||
## Usage flag: -help
|
||||
## Help flag:
|
||||
|
||||
The package also contains a help context menu. This comes default and can be called like so
|
||||
The package also contains a help context menu. This comes default and can be called like so (**Note:** You can also use "-h" or "--help"):
|
||||
|
||||
```txt
|
||||
./<program name> -help
|
||||
@@ -52,6 +52,10 @@ The package also contains a help context menu. This comes default and can be cal
|
||||
|
||||
**Note:** You can set the boolean *FORCE_HELP_ON_EMPTY_ARGS* to allow for the help context menu to be displayed when no arguments are passed
|
||||
|
||||
## Custom Flag Format
|
||||
|
||||
A custom flag format can be created by overwriting the strings *FLAG_START_CHAR* and *FLAG_SEP_CHAR*. These cannot be the same however!
|
||||
|
||||
## Custom Allocator
|
||||
|
||||
If needed, you are able to setup a custom allocator like so:
|
||||
|
||||
+36
-2
@@ -1,3 +1,25 @@
|
||||
/*
|
||||
zlib License
|
||||
|
||||
(C) 2026 Simon Kellet
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#+vet explicit-allocators
|
||||
package fleg
|
||||
|
||||
@@ -26,7 +48,10 @@ Flag :: struct {
|
||||
parsed: bool, // sck: flag to check if it has been parsed
|
||||
}
|
||||
|
||||
// Global dynamic array of Flags
|
||||
all_flags: [dynamic]Flag
|
||||
|
||||
// runtime allocator for the flags
|
||||
flag_allocator: runtime.Allocator
|
||||
|
||||
// sck: Used to create a customer allocator
|
||||
@@ -95,21 +120,30 @@ Float64Var :: proc(ptr: ^f64, name: string, default: f64, usage: string) {
|
||||
}
|
||||
|
||||
parse_flags :: proc() {
|
||||
// sck: We cannot have the start and seperator formats being the same
|
||||
// ...leads to parsing errors...
|
||||
if FLAG_START_CHAR == FLAG_SEP_CHAR {
|
||||
fmt.println("[ERROR]: FLAG_START_CHAR and FLAG_SEP_CHAR cannot be the same!")
|
||||
fmt.printfln("\t FLAG_START_CHAR= \"%s\"\t FLAG_SEP_CHAR= \"%s\"", FLAG_START_CHAR, FLAG_SEP_CHAR)
|
||||
os.exit(1)
|
||||
}
|
||||
if len(os.args) < 2 && FORCE_HELP_ON_EMPTY_ARGS {print_usage()}
|
||||
|
||||
for &a in os.args {
|
||||
if a == "-h" || a == "-help" || a == "--help" {print_usage()}
|
||||
// sck: if the user has a custom flag format, copy that to the help flag too.
|
||||
if a == fmt.aprintf("%s%s", FLAG_START_CHAR, "help", allocator = flag_allocator) {print_usage()}
|
||||
if a == fmt.aprintf("%s%s", FLAG_START_CHAR, "h", allocator = flag_allocator) {print_usage()}
|
||||
|
||||
if a == os.args[0] {continue}
|
||||
|
||||
for &f in all_flags {
|
||||
if f.parsed {continue}
|
||||
//a = strings.trim_prefix(a, "-")
|
||||
a = strings.trim_prefix(a, FLAG_START_CHAR)
|
||||
|
||||
name := a
|
||||
value: string
|
||||
|
||||
//split := strings.index_any(a, "=")
|
||||
split := strings.index_any(a, FLAG_SEP_CHAR)
|
||||
if split >= 0 {
|
||||
name = a[:split]
|
||||
|
||||
@@ -14,9 +14,9 @@ main :: proc(){
|
||||
fleg.init_custom_allocator(arena_alloc)
|
||||
*/
|
||||
|
||||
fleg.FLAG_START_CHAR = "--"
|
||||
fleg.FLAG_SEP_CHAR = ":"
|
||||
fleg.FORCE_HELP_ON_EMPTY_ARGS = true
|
||||
fleg.FLAG_START_CHAR = ":"
|
||||
fleg.FLAG_SEP_CHAR = "="
|
||||
//fleg.FORCE_HELP_ON_EMPTY_ARGS = true
|
||||
|
||||
defer fleg.destroy()
|
||||
isRunning: bool
|
||||
|
||||
Reference in New Issue
Block a user