Commit Graph

24 Commits

Author SHA1 Message Date
Thibaut Brandscheid
755876e9b6 fix most clippy warnings 2018-06-19 17:17:39 +02:00
Benjamin Sago
a8bf990674 Tie value suggestions to their arguments
This commit changes the definition of Arg so that it knows about which values it can accept, and can display them in the help text. They were already being shown in the help text, but they were passed in separately, so one argument could show two different sets of options if it wanted. Now, the argument itself knows whether there are suggestions, so it doesn’t have to be passed in separately.

This means we can use it for other things, including listing choices when an option is missed out, without having to repeat the list.

With Misfire::BadArgument now only having two fields, it’s not worth using a constructor function anymore.
2017-09-14 01:22:37 +01:00
Benjamin Sago
b286676667 Add actual error messages for the error messages
The annoying part is trying to format!() an OsStr.
2017-08-10 23:34:39 +01:00
Benjamin Sago
6b309d5cfc Make SizeFormat lenient, and add tests
This changes the SizeFormat option parser from its old, strict-by-default behaviour (where passing both --bytes and --binary would be an error) to the new, use-the-last-argument behaviour (where passing --bytes --binary would use --binary because it came later).

Doing this meant adding functionality to Matches so that it could return *which* argument matched. Previously, the order of --bytes and --binary didn’t matter, because they couldn’t both be present, but now it does.
2017-08-09 19:18:31 +01:00
Benjamin Sago
ff497b52e5 Be stricter in strict mode
Now the code actually starts to use the Strictness flag that was added in the earlier commit! Well, the *code* doesn’t, but the tests do: the macros that create the test cases now have a parameter for which tests they should run. It’s usually ‘Both’ for both strict mode and default mode, but can be specified to only run in one, for when the results differ (usually when options override one another)

The downside to strict mode is that, now, *any* call to `matches.has` or `matches.get` could fail, because an option could have been specified twice, and this is the place where those are checked for. This makes the code a little less ergonomic in places, but that’s what the ? operator is for. The only place this has really had an effect is in `Classify::deduce`, which used to just return a boolean but can now fail.

In order to more thoroughly test the mode, some of the older parts of the code can now act more strict. For example, `TerminalColours::deduce` will now use the last-given option rather than searching for “colours” before “colors”.

Help and Version continue doing their own thing.
2017-08-09 09:21:29 +01:00
Benjamin Sago
00379cce63 Thread Strictness through the parser
The value is ignored, but this broke quite a lot of tests that assumed MatchedFlags had only one field.

Parsing tests have to have OsStr flags because I couldn’t get that part working right, but in general, some tests now re-use common functionality too.
2017-08-08 09:18:17 +01:00
Benjamin Sago
6759a5f97e Temporarily hush warnings 2017-08-05 19:56:42 +01:00
Benjamin Sago
cdad6bb960 Make building the matches more bearable
Now, building the two result vectors doesn’t have to go through at least one field.
2017-08-05 19:15:27 +01:00
Benjamin Sago
9872eba821 Separate the matched flags from the free strings
Originally, both the matched flags and the list of free strings were returned from the parsing function and then passed around to every type that had a ‘deduce’ method. This worked, but the list of free strings was carried around with it, never used.

Now, only the flags are passed around. They’re in a new struct which has the methods the Matches had.

Both of Matches’s fields are now just data, and all of the methods on MatchedFlags don’t ignore any fields, so it’s more cohesive, at least I think that’s the word.

Building up the MatchedFlags is a bit more annoying though because the vector is now hidden behind a field.
2017-08-05 19:11:00 +01:00
Benjamin Sago
0456e7cfbd Document and organise the parser module 2017-08-05 17:46:38 +01:00
Benjamin Sago
3331feaee8 Turning off unused_variables revealed a warning 2017-08-05 15:40:15 +01:00
Benjamin Sago
45e1cb77a4 Streamline parser tests 2017-07-26 20:33:59 +01:00
Benjamin Sago
2d1f462bfa Switch to the new options parser
This commit removes the dependency on the ‘getopts’ crate entirely, and re-writes all its uses to use the new options parser instead.

As expected there are casualties galore:

- We now need to collect the options into a vector at the start, so we can use references to them, knowing they’ll be stored *somewhere*.
- Because OsString isn’t Display, its Debug impl gets used instead. (This is hopefully temporary)
- Options that take values (such as ‘sort’ or ‘time-style’) now parse those values with ‘to_string_lossy’. The ‘lossy’ part means “I’m at a loss for what to do here”
- Error messages got a lot worse, but “--tree --all --all” is now a special case of error rather than just another Misfire::Useless.
- Some tests had to be re-written to deal with the fact that the parser works with references.
- ParseError loses its lifetime and owns its contents, to avoid having to attach <'a> to Misfire.
- The parser now takes an iterator instead of a slice.
- OsStrings can’t be ‘match’ patterns, so the code devolves to using long Eq chains instead.
- Make a change to the xtest that assumed an input argument with invalid UTF-8 in was always an error to stderr, when that now in fact works!
- Fix a bug in Vagrant where ‘exa’ and ‘rexa’ didn’t properly escape filenames with spaces in.
2017-07-26 17:48:18 +01:00
Benjamin Sago
8d96be7f6a Document the parsing decisions
Even though these can’t actually be viewed with `cargo doc` yet, they’re still good to have around.
2017-07-22 18:06:05 +01:00
Benjamin Sago
5227f09f5b Macro-ify the matching tests 2017-07-13 11:51:58 +01:00
Benjamin Sago
53dc370a41 Search for long options through matches
Casualty here was that you can’t have static values reference one another directly, so the static args slice had to be turned into a slice *of references* rather than of values. No big deal, just have to write & a few more times.
2017-07-13 11:51:50 +01:00
Benjamin Sago
e08d7fe524 Test for unknown arguments 2017-07-13 10:21:10 +01:00
Benjamin Sago
0e944d7b74 Macroify parser tests 2017-07-13 10:12:38 +01:00
Benjamin Sago
c9f6c45040 Add missing short arg functionality 2017-07-13 00:51:05 +01:00
Benjamin Sago
c41005a121 Change a bunch of variable names 2017-07-12 23:17:43 +01:00
Benjamin Sago
9bbe2d9816 Throw error immediately on lookup
Every time looking up an argument fails, it returns an error. We might as well just move this into the lookup function.
2017-07-12 22:59:04 +01:00
Benjamin Sago
c18302e46e This function was used in both 2017-07-12 22:49:10 +01:00
Benjamin Sago
62b85e3e93 Add some tests for split_equals 2017-07-12 22:47:17 +01:00
Benjamin Sago
bf643c65fe Start work on our own options parser
All the tests pass, but only half the functionality is there right now.
2017-07-12 12:03:07 +01:00