whitespace-rs icon indicating copy to clipboard operation
whitespace-rs copied to clipboard

Exit with status 1 on error; fix usage typos

Open thaliaarchi opened this issue 3 years ago • 3 comments

This PR makes a couple of minor fixes in the program --help.

  • Updates the executable name to wsc in the summary
  • --no-implicit-exit was missing from the summary
  • The short option for --count was documented as -d, but expects -c

If you're curious, I've listed Whitespacers in my Whitespace Corpus, which tracks known Whitespace tools.

thaliaarchi avatar May 24 '21 10:05 thaliaarchi

I just pushed a commit which changes main to exit with a status of 1 on error to make it more useful within scripts.

thaliaarchi avatar May 25 '21 23:05 thaliaarchi

Thanks for the pull request!

Only comment: the exit code issue can probably be implemented cleaner using main return types (https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/question-mark-in-main-and-tests.html) that were introduced in 1.26.

CensoredUsername avatar May 26 '21 12:05 CensoredUsername

It looks like when returning a Result from main, the Debug representation is printed, which is not desirable. I think it has to be done manually as-is, unless you know of a better way.

$ wsc x
Error: Os { code: 2, kind: NotFound, message: "No such file or directory" }
$ wsc -f asm README.md
Error: WsError { message: "Unrecognized symbol #", kind: ParseError(1, 1, 0), location: None, cause: None }
$ wsc -p -p README.md
Error: "Option --perf was specified twice"
$ wsc --help
Error: "Usage: wsc PROGRAM [-h | -i INFILE | -o OUTFILE | [-t | -e STRATEGY | -d DUMPFILE | -c] | -f FORMAT | -p | --ignore-overflow | --unchecked-heap | --no-fallback | --no-implicit-exit]\n\nwsc - A really fast whitespace JIT-compiler.\n\nRequired arguments:\n    PROGRAM                 The whitespace program to execute\nOptions:\n    -h --help               Display this message\n    -f --format  FORMAT     Input file format. The default is plain whitespace. Options are:\n        ws|whitespace       Plain whitespace.\n        asm|assembly        A human-readable assembly format. A description can be found below.\n ...

Here is the change:

--- a/src/main.rs
+++ b/src/main.rs
@@ -4,7 +4,6 @@ use std::env;
 use std::error::Error;
 use std::io::{self, Write, Read, BufRead};
 use std::fs::File;
-use std::process;
 use std::time::Instant;
 
 use whitespacers::{Program, Interpreter, Options, debug_compile};
@@ -45,17 +44,7 @@ enum Strategy {
     Count
 }
 
-fn main() {
-    process::exit(match console_main() {
-        Ok(()) => 0,
-        Err(s) => {
-            eprintln!("Error: {}", s);
-            1
-        }
-    });
-}
-
-fn console_main() -> Result<(), Box<dyn Error>> {
+fn main() -> Result<(), Box<dyn Error>> {
     let time_start = Instant::now();
 
     let args = parse_args()?;

thaliaarchi avatar May 27 '21 01:05 thaliaarchi