From 9950fb1ec1c98ddc9227936200be6a80fbc94c62 Mon Sep 17 00:00:00 2001 From: hchac Date: Sun, 3 Mar 2024 22:49:34 -0500 Subject: Include `-stdin` in args to parse. --- tools/odinfmt/main.odin | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/odinfmt/main.odin b/tools/odinfmt/main.odin index e5ca79b..8e0886b 100644 --- a/tools/odinfmt/main.odin +++ b/tools/odinfmt/main.odin @@ -102,13 +102,22 @@ main :: proc() { os.exit(1) } - if res := flag.parse(args, os.args[1:len(os.args) - 1]); res != .None { + // When running `$ odinfmt some_odin_file.odin`, the file shouldn't be passed to + // `flag.parse`. But, when running `$ odinfmt -stdin`, we do want to pass `-stdin` to + // `flag.parse`. Need to check if last arg is a flag, and if so, include it in the + // args to parse. + args_to_parse := os.args[1:len(os.args) - 1] + path := os.args[len(os.args) - 1] + if strings.has_prefix(os.args[len(os.args) - 1], "-") { + args_to_parse = os.args[1:] + path = "." // if no file was specified, use current directory as the starting path to look for `odinfmt.json` + } + + if res := flag.parse(args, args_to_parse); res != .None { print_arg_error(os.args, res) os.exit(1) } - path := os.args[len(os.args) - 1] - tick_time := time.tick_now() write_failure := false @@ -204,3 +213,4 @@ main :: proc() { os.exit(1 if write_failure else 0) } + -- cgit v1.2.3