Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1b8884554 | ||
|
|
a52445e536 | ||
|
|
c93be3493d |
@@ -20,6 +20,15 @@ This a very simple implementation of Go-like flags in Odin
|
|||||||
fleg.parse_flags()
|
fleg.parse_flags()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also marks flags as **required"**. These will still be parsed, however will hault the application until they are passed:
|
||||||
|
|
||||||
|
```odin
|
||||||
|
import "fleg"
|
||||||
|
isRunning: bool
|
||||||
|
fleg.BoolVar(&isRunning, "isRunning", true, "A select piece of code is running!", required = true)
|
||||||
|
fleg.parse_flags()
|
||||||
|
```
|
||||||
|
|
||||||
***Supports: bool, int, string, f32***
|
***Supports: bool, int, string, f32***
|
||||||
|
|
||||||
And on the command line, you pass flags like so:
|
And on the command line, you pass flags like so:
|
||||||
@@ -54,7 +63,20 @@ The package also contains a help context menu. This comes default and can be cal
|
|||||||
|
|
||||||
## Custom Flag Format
|
## 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!
|
A custom flag format can be created by overwriting the strings *FLAG_START_CHAR* and *FLAG_SEP_CHAR*. (These *cannot* be the same however!)
|
||||||
|
|
||||||
|
```odin
|
||||||
|
fleg.FLAG_START_CHAR = "--"
|
||||||
|
fleg.FLAG_SEP_CHAR = ":"
|
||||||
|
```
|
||||||
|
|
||||||
|
Flags will now be in the format:
|
||||||
|
|
||||||
|
```txt
|
||||||
|
--isString:"......"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note:** Any change to the format of the flags is also relfected upon the "help" flag.
|
||||||
|
|
||||||
## Custom Allocator
|
## Custom Allocator
|
||||||
|
|
||||||
@@ -70,3 +92,10 @@ If needed, you are able to setup a custom allocator like so:
|
|||||||
fleg.init_custom_allocator(arena_alloc)
|
fleg.init_custom_allocator(arena_alloc)
|
||||||
defer fleg.destroy() //clean up flags
|
defer fleg.destroy() //clean up flags
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
|
||||||
|
* More type support
|
||||||
|
* Better logging of INFO and ERROR's
|
||||||
|
* String validation (perhaps not needed?)
|
||||||
|
* Array support (e.g. **--isArray:{4,5,6,7}**)
|
||||||
+5
-3
@@ -5,6 +5,7 @@ import vmem "core:mem/virtual"
|
|||||||
import "fleg"
|
import "fleg"
|
||||||
|
|
||||||
main :: proc(){
|
main :: proc(){
|
||||||
|
/*
|
||||||
arena: vmem.Arena
|
arena: vmem.Arena
|
||||||
arena_err := vmem.arena_init_growing(&arena)
|
arena_err := vmem.arena_init_growing(&arena)
|
||||||
ensure(arena_err == nil)
|
ensure(arena_err == nil)
|
||||||
@@ -13,10 +14,11 @@ main :: proc(){
|
|||||||
|
|
||||||
fleg.init_custom_allocator(arena_alloc)
|
fleg.init_custom_allocator(arena_alloc)
|
||||||
defer fleg.destroy()
|
defer fleg.destroy()
|
||||||
|
*/
|
||||||
|
|
||||||
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
|
||||||
@@ -24,8 +26,8 @@ main :: proc(){
|
|||||||
isString: string
|
isString: string
|
||||||
isFloat: f32
|
isFloat: f32
|
||||||
|
|
||||||
fleg.BoolVar(&isRunning, "isRunning", true, "A select piece of code is running!")
|
fleg.BoolVar(&isRunning, "isRunning", false, "A select piece of code is running!", required = true)
|
||||||
fleg.IntVar(&isNumber, "isNumber", 3434, "A number!")
|
fleg.IntVar(&isNumber, "isNumber", 3434, "A number!", required = true)
|
||||||
fleg.StringVar(&isString, "isString", "Hellope!", "A string!")
|
fleg.StringVar(&isString, "isString", "Hellope!", "A string!")
|
||||||
fleg.Float32Var(&isFloat, "isFloat", 3.1415, "A float!")
|
fleg.Float32Var(&isFloat, "isFloat", 3.1415, "A float!")
|
||||||
fleg.parse_flags()
|
fleg.parse_flags()
|
||||||
|
|||||||
+35
-21
@@ -46,6 +46,7 @@ Flag :: struct {
|
|||||||
value: Flag_Value_Ptr,
|
value: Flag_Value_Ptr,
|
||||||
usage: string,
|
usage: string,
|
||||||
parsed: bool, // sck: flag to check if it has been parsed
|
parsed: bool, // sck: flag to check if it has been parsed
|
||||||
|
required: bool, // sck: required flag
|
||||||
}
|
}
|
||||||
|
|
||||||
// Global dynamic array of Flags
|
// Global dynamic array of Flags
|
||||||
@@ -69,14 +70,19 @@ destroy :: proc() {
|
|||||||
|
|
||||||
print_flags :: proc() {
|
print_flags :: proc() {
|
||||||
for f in all_flags {
|
for f in all_flags {
|
||||||
fmt.printfln("%s: %v: %s", f.name, f.value, f.usage)
|
fmt.printfln("%s: %s (required=%v)", f.name, f.usage, f.required)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print_flag_format :: proc(){
|
||||||
|
fmt.printfln("Want %s<flag>%s<value>\n", FLAG_START_CHAR, FLAG_SEP_CHAR)
|
||||||
|
}
|
||||||
|
|
||||||
print_usage :: proc() {
|
print_usage :: proc() {
|
||||||
fmt.println("\tUsage: ")
|
fmt.println("\tUsage: ")
|
||||||
fmt.println("\n\tFlag format for this program:")
|
fmt.println("\n\tFlag format for this program:")
|
||||||
fmt.printfln("\t%s<flag>%s<value>\n", FLAG_START_CHAR, FLAG_SEP_CHAR)
|
fmt.printfln("\t%s<flag>%s<value>\n", FLAG_START_CHAR, FLAG_SEP_CHAR)
|
||||||
|
|
||||||
for f in all_flags {
|
for f in all_flags {
|
||||||
switch v in f.value {
|
switch v in f.value {
|
||||||
case ^bool:
|
case ^bool:
|
||||||
@@ -91,37 +97,36 @@ print_usage :: proc() {
|
|||||||
fmt.printfln("\t%s%s:\n\t\t%s (default: %f)\n", FLAG_START_CHAR, f.name, f.usage, v^)
|
fmt.printfln("\t%s%s:\n\t\t%s (default: %f)\n", FLAG_START_CHAR, f.name, f.usage, v^)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
os.exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BoolVar :: proc(ptr: ^bool, name: string, default: bool, usage: string) {
|
BoolVar :: proc(ptr: ^bool, name: string, default: bool, usage: string, required := false) {
|
||||||
ptr^ = default
|
ptr^ = default
|
||||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
append(&all_flags, Flag{name = name, value = ptr, usage = usage, required = required})
|
||||||
}
|
}
|
||||||
|
|
||||||
IntVar :: proc(ptr: ^int, name: string, default: int, usage: string) {
|
IntVar :: proc(ptr: ^int, name: string, default: int, usage: string, required := false) {
|
||||||
ptr^ = default
|
ptr^ = default
|
||||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
append(&all_flags, Flag{name = name, value = ptr, usage = usage, required = required})
|
||||||
}
|
}
|
||||||
|
|
||||||
StringVar :: proc(ptr: ^string, name: string, default: string, usage: string) {
|
StringVar :: proc(ptr: ^string, name: string, default: string, usage: string, required := false) {
|
||||||
ptr^ = default
|
ptr^ = default
|
||||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
append(&all_flags, Flag{name = name, value = ptr, usage = usage, required = required})
|
||||||
}
|
}
|
||||||
|
|
||||||
Float32Var :: proc(ptr: ^f32, name: string, default: f32, usage: string) {
|
Float32Var :: proc(ptr: ^f32, name: string, default: f32, usage: string, required := false) {
|
||||||
ptr^ = default
|
ptr^ = default
|
||||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
append(&all_flags, Flag{name = name, value = ptr, usage = usage, required = required})
|
||||||
}
|
}
|
||||||
|
|
||||||
Float64Var :: proc(ptr: ^f64, name: string, default: f64, usage: string) {
|
Float64Var :: proc(ptr: ^f64, name: string, default: f64, usage: string, required := false) {
|
||||||
ptr^ = default
|
ptr^ = default
|
||||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
append(&all_flags, Flag{name = name, value = ptr, usage = usage, required = required})
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_flags :: proc() {
|
parse_flags :: proc() {
|
||||||
|
defer destroy()
|
||||||
// sck: We cannot have the start and seperator formats being the same
|
// sck: We cannot have the start and seperator formats being the same
|
||||||
// ...leads to parsing errors...
|
|
||||||
if FLAG_START_CHAR == FLAG_SEP_CHAR {
|
if FLAG_START_CHAR == FLAG_SEP_CHAR {
|
||||||
fmt.println("[ERROR]: FLAG_START_CHAR and FLAG_SEP_CHAR cannot be the same!")
|
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)
|
fmt.printfln("\t FLAG_START_CHAR= \"%s\"\t FLAG_SEP_CHAR= \"%s\"", FLAG_START_CHAR, FLAG_SEP_CHAR)
|
||||||
@@ -129,18 +134,18 @@ parse_flags :: proc() {
|
|||||||
}
|
}
|
||||||
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 {
|
// sck: skip the first arg
|
||||||
if a == "-h" || a == "-help" || a == "--help" {print_usage()}
|
for &a in os.args[1:] {
|
||||||
|
if a == "-h" || a == "-help" || a == "--help" {print_usage(); os.exit(0)}
|
||||||
// sck: if the user has a custom flag format, copy that to the help flag too.
|
// 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, "help", allocator = flag_allocator) {print_usage(); os.exit(0)}
|
||||||
if a == fmt.aprintf("%s%s", FLAG_START_CHAR, "h", allocator = flag_allocator) {print_usage()}
|
if a == fmt.aprintf("%s%s", FLAG_START_CHAR, "h", allocator = flag_allocator) {print_usage(); os.exit(0)}
|
||||||
|
|
||||||
if a == os.args[0] {continue}
|
|
||||||
|
|
||||||
|
// Begin parsing flags...
|
||||||
for &f in all_flags {
|
for &f in all_flags {
|
||||||
if f.parsed {continue}
|
if f.parsed {continue}
|
||||||
a = strings.trim_prefix(a, FLAG_START_CHAR)
|
|
||||||
|
|
||||||
|
a = strings.trim_prefix(a, FLAG_START_CHAR)
|
||||||
name := a
|
name := a
|
||||||
value: string
|
value: string
|
||||||
|
|
||||||
@@ -151,10 +156,11 @@ parse_flags :: proc() {
|
|||||||
} else {
|
} else {
|
||||||
name = a
|
name = a
|
||||||
fmt.printf("[INFO]: Could not read flag: %s. ", name)
|
fmt.printf("[INFO]: Could not read flag: %s. ", name)
|
||||||
fmt.printfln("Want %s<flag>%s<value>\n", FLAG_START_CHAR, FLAG_SEP_CHAR)
|
print_flag_format()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sck: We are going to parse all the flags
|
||||||
if name == f.name && !f.parsed {
|
if name == f.name && !f.parsed {
|
||||||
f.parsed = true
|
f.parsed = true
|
||||||
switch &v in f.value {
|
switch &v in f.value {
|
||||||
@@ -206,4 +212,12 @@ parse_flags :: proc() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for f in all_flags {
|
||||||
|
if f.required && !f.parsed {
|
||||||
|
fmt.fprintfln(os.stderr, "[ERROR] Missing required flag: %s%s", FLAG_START_CHAR, f.name)
|
||||||
|
print_usage()
|
||||||
|
os.exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user