Compare commits

..
4 Commits
Author SHA1 Message Date
Simon Kellet 5128851965 added zlib 2026-07-09 16:37:03 +01:00
Simon Kellet 28329fa0b7 example custom flag format 2026-07-09 16:36:25 +01:00
Simon Kellet c675f6b659 added doc for custom flag foramt 2026-07-09 16:36:04 +01:00
Simon Kellet 49eaaeabf5 help flag now follows custom formatting 2026-07-09 16:32:34 +01:00
3 changed files with 45 additions and 7 deletions
+6 -2
View File
@@ -28,9 +28,9 @@ And on the command line, you pass flags like so:
./<program name> -isNumber=45 -isRunning=false ...... ./<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 ```txt
./<program name> -help ./<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 **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 ## Custom Allocator
If needed, you are able to setup a custom allocator like so: If needed, you are able to setup a custom allocator like so:
+36 -2
View File
@@ -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 #+vet explicit-allocators
package fleg package fleg
@@ -26,7 +48,10 @@ Flag :: struct {
parsed: bool, // sck: flag to check if it has been parsed parsed: bool, // sck: flag to check if it has been parsed
} }
// Global dynamic array of Flags
all_flags: [dynamic]Flag all_flags: [dynamic]Flag
// runtime allocator for the flags
flag_allocator: runtime.Allocator flag_allocator: runtime.Allocator
// sck: Used to create a customer 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() { 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()} if len(os.args) < 2 && FORCE_HELP_ON_EMPTY_ARGS {print_usage()}
for &a in os.args { for &a in os.args {
if a == "-h" || a == "-help" || a == "--help" {print_usage()} 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} if a == os.args[0] {continue}
for &f in all_flags { for &f in all_flags {
if f.parsed {continue} if f.parsed {continue}
//a = strings.trim_prefix(a, "-")
a = strings.trim_prefix(a, FLAG_START_CHAR) a = strings.trim_prefix(a, FLAG_START_CHAR)
name := a name := a
value: string value: string
//split := strings.index_any(a, "=")
split := strings.index_any(a, FLAG_SEP_CHAR) split := strings.index_any(a, FLAG_SEP_CHAR)
if split >= 0 { if split >= 0 {
name = a[:split] name = a[:split]
+3 -3
View File
@@ -14,9 +14,9 @@ main :: proc(){
fleg.init_custom_allocator(arena_alloc) fleg.init_custom_allocator(arena_alloc)
*/ */
fleg.FLAG_START_CHAR = "--" fleg.FLAG_START_CHAR = ":"
fleg.FLAG_SEP_CHAR = ":" fleg.FLAG_SEP_CHAR = "="
fleg.FORCE_HELP_ON_EMPTY_ARGS = true //fleg.FORCE_HELP_ON_EMPTY_ARGS = true
defer fleg.destroy() defer fleg.destroy()
isRunning: bool isRunning: bool