From e6a3b84466e13abd74e81068ee7f06f7c4c569a3 Mon Sep 17 00:00:00 2001 From: godalming123 <68993177+godalming123@users.noreply.github.com> Date: Thu, 18 Sep 2025 19:49:51 +0100 Subject: Change the behavior back so that not specifying the path to format cuases an error unless odinfmt is formatting from stdin --- tools/odinfmt/main.odin | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'tools') diff --git a/tools/odinfmt/main.odin b/tools/odinfmt/main.odin index 50777e2..3470ca5 100644 --- a/tools/odinfmt/main.odin +++ b/tools/odinfmt/main.odin @@ -3,6 +3,7 @@ package odinfmt import "core:encoding/json" import "core:flags" import "core:fmt" +import "core:io" import "core:mem" import "core:odin/tokenizer" import "core:os" @@ -12,6 +13,12 @@ import "core:time" import "src:odin/format" import "src:odin/printer" +Args :: struct { + write: bool `args:"name=w" usage:"write the new format to file"`, + stdin: bool `usage:"formats code from standard input"`, + path: string `args:"pos=0" usage:"set the file or directory to format"`, +} + format_file :: proc(filepath: string, config: printer.Config, allocator := context.allocator) -> (string, bool) { if data, ok := os.read_entire_file(filepath, allocator); ok { return format.format(filepath, string(data), config, {.Optional_Semicolons}, allocator) @@ -44,16 +51,19 @@ main :: proc() { init_global_temporary_allocator(mem.Megabyte * 20) //enough space for the walk - args: struct { - write: bool `args:"name=w" usage:"write the new format to file"`, - stdin: bool `usage:"formats code from standard input"`, - path: string `args:"pos=0" usage:"set the file or directory to format"`, - } + args: Args flags.parse_or_exit(&args, os.args) - // if no file was specified, use current directory as the starting path to look for `odinfmt.json` + // only allow the path to not be specified when formatting from stdin if args.path == "" { - args.path = "." + if args.stdin { + // use current directory as the starting path to look for `odinfmt.json` + args.path = "." + } else { + fmt.fprint(os.stderr, "Missing path to format\n") + flags.write_usage(os.stream_from_handle(os.stderr), Args, os.args[0]) + os.exit(1) + } } tick_time := time.tick_now() -- cgit v1.2.3