Add more cli checks

This commit is contained in:
Riyyi
2026-08-02 17:08:18 +02:00
parent f36a50fca3
commit 87d20f2ae5
4 changed files with 70 additions and 20 deletions
+40 -7
View File
@@ -3,6 +3,7 @@ package cli
import "core:flags" import "core:flags"
import "core:fmt" import "core:fmt"
import "core:os" import "core:os"
import "core:strings"
Options :: struct { Options :: struct {
verbose: bool `args:"name=verbose" usage:"Enable verbose output"`, verbose: bool `args:"name=verbose" usage:"Enable verbose output"`,
@@ -13,7 +14,7 @@ Options :: struct {
// overflow: [dynamic]string, // overflow: [dynamic]string,
} }
parse :: proc() -> Options { parse :: proc(working_dir: string) -> Options {
opts: Options opts: Options
err := flags.parse(&opts, os.args[1:], .Unix) err := flags.parse(&opts, os.args[1:], .Unix)
@@ -23,16 +24,48 @@ parse :: proc() -> Options {
os.exit(1) os.exit(1)
} }
verify(opts, working_dir)
return opts
}
@(private)
verify :: proc(opts: Options, working_dir: string) {
if opts.compression > 12 { if opts.compression > 12 {
err := flags.Validation_Error { throw_error(
message = fmt.tprintf(
`Invalid compression "%d". Compression higher than allowed maximum.`, `Invalid compression "%d". Compression higher than allowed maximum.`,
opts.compression, opts.compression,
), )
}
input_path := os.name(opts.input)
if !os.is_dir(input_path) {
throw_error(`Invalid input "%v". Should be a directory`, input_path)
}
if opts.output != nil {
output_path := os.name(opts.output)
if !os.is_dir(output_path) {
throw_error(
`Invalid output "%v". Should be a directory`,
output_path,
)
}
}
if !strings.has_prefix(input_path, working_dir) {
throw_error(
`Invalid input "%v". Should be a subdirectory of the working directory.`,
input_path,
)
}
}
@(private)
throw_error :: proc(fmt_str: string, args: ..any) {
err := flags.Validation_Error {
message = fmt.tprintf(fmt_str, ..args),
} }
flags.print_errors(typeid_of(Options), err, os.args[0], .Unix) flags.print_errors(typeid_of(Options), err, os.args[0], .Unix)
os.exit(1) os.exit(1)
} }
return opts
}
+21
View File
@@ -1,7 +1,28 @@
package file package file
import "core:fmt"
import "core:os"
// TODO: // TODO:
// - list files in directory // - list files in directory
// - get relative path // - get relative path
// - read file contents // - read file contents
// - write file contents // - write file contents
working_dir: string
store_working_dir :: proc(allocator := context.allocator) {
wd, err := os.get_working_directory(allocator)
if err != nil {
fmt.eprintln("error: get working directory failed:", err)
os.exit(1)
}
working_dir = wd
}
remove_working_dir :: proc() {
if working_dir != "" {
delete(working_dir)
working_dir = ""
}
}
+1 -8
View File
@@ -34,20 +34,13 @@ list_dir_recursive_by_path :: proc(
@(private) @(private)
list_dir_recursive_by_path_impl :: proc( list_dir_recursive_by_path_impl :: proc(
path: string, path: string,
allocator: runtime.Allocator allocator: runtime.Allocator,
) -> [dynamic]DirectoryEntry { ) -> [dynamic]DirectoryEntry {
if !os.is_dir(path) { if !os.is_dir(path) {
fmt.eprintln("error: path is not a directory:", path) fmt.eprintln("error: path is not a directory:", path)
os.exit(1) os.exit(1)
} }
working_dir, wd_err := os.get_working_directory(allocator)
if wd_err != nil {
fmt.eprintln("error: get working directory failed:", wd_err)
os.exit(1)
}
defer delete(working_dir, allocator)
result := make([dynamic]DirectoryEntry, allocator) result := make([dynamic]DirectoryEntry, allocator)
queue := make([dynamic]string, 0, 1, allocator) // reserve 1 slot queue := make([dynamic]string, 0, 1, allocator) // reserve 1 slot
+4 -1
View File
@@ -8,7 +8,10 @@ import "src:file"
VERSION :: #config(VERSION, "dev") VERSION :: #config(VERSION, "dev")
main :: proc() { main :: proc() {
opts := cli.parse() file.store_working_dir()
defer file.remove_working_dir()
opts := cli.parse(file.working_dir)
fmt.println("verbose:", opts.verbose) fmt.println("verbose:", opts.verbose)
fmt.println("compression:", opts.compression) fmt.println("compression:", opts.compression)