Commit Graph

79 Commits

Author SHA1 Message Date
Jun Wu
30f293314c clidispatch: support explicitly named arguments in define_flags!
Summary:
This makes it possible to use explicitly named arguments. It can simplify the
code a bit by moving the error handling (incorrect number of arguments) to the
derived code.

Reviewed By: xavierd

Differential Revision: D16905460

fbshipit-source-id: 93dc58d684bb52cd2f9bddc25108ef0c500f81db
2019-08-28 19:26:28 -07:00
Jun Wu
ed3f36bb85 cliparser: return errors if unexpected arguments are provided
Summary:
If there is no `#[args]` field set, the command does not need arguments. In
that case, just error out directly. This can simplify command implementations.

Reviewed By: xavierd

Differential Revision: D16905461

fbshipit-source-id: f4a2d03cf032f34d1b5794010774ff80eb88e1cd
2019-08-28 19:26:28 -07:00
Jun Wu
3c881c88d7 clidispatch: change From to TryFrom for flags parsing
Summary: This make it possible for the flags parsing to return an error.

Reviewed By: xavierd

Differential Revision: D16905462

fbshipit-source-id: 06a70ee7c72132ebb2df453a740b58e6b46dfba2
2019-08-28 19:26:27 -07:00
Jun Wu
06ff75a799 cliparser: add #[command_name] to define_flags!
Summary:
Most commands do not care about the "command name" (aka. arg0). Only commands
sharing a similar implementation (ex. hg up/down/bottom/top) would need it.
Make it optional and make `args` not containing the command name.

Reviewed By: xavierd

Differential Revision: D16905459

fbshipit-source-id: 0a786731ebce2580e116fd50aad75062b03a1fa3
2019-08-28 19:26:27 -07:00
Jun Wu
48e21aebc8 cliparser: add names to define_flags! internal fields
Summary: This makes the code easier to read as we add more fields to the macro state.

Reviewed By: xavierd

Differential Revision: D16905458

fbshipit-source-id: a3bc4dc1accf21cf491921469482f86aec16321b
2019-08-28 19:26:27 -07:00
Jun Wu
7cce00694c cliparser: use lowercase text in errors
Summary:
This is more conistent with Mercurial style. And make them usable directly in
Rust code.

Reviewed By: sfilipco

Differential Revision: D16796397

fbshipit-source-id: 9016ea2b09fdf96b2b54138f5c8405caf96390f7
2019-08-21 12:16:36 -07:00
Jun Wu
253b03e87c cliparser: sort possibilities in AmbiguousCommand error
Summary:
This makes the "content" of the error stable. It is used in a later diff where
AmbiguousCommand gets handled by Rust directly instead of falling back to
Python dispatch.py.

Reviewed By: sfilipco

Differential Revision: D16796404

fbshipit-source-id: c439db14ec83c76c4762d3c627bfce1ea44bccf4
2019-08-21 12:16:36 -07:00
Jun Wu
76cb250fcd cliparser: store positional args in structs
Summary:
This makes it possible to use one structure instead of a structure + another
`args` list.

The current version only supports a `Vec<String>` field and there is no verification
about how many arguments that `Vec<String>` can have. In the future we can add:
- Verification about arguments. Potentially change `From<ParseOutput>` to `TryFrom<ParseOutput>`.
- Individual named arguments as individual fields.

Reviewed By: sfilipco

Differential Revision: D16733272

fbshipit-source-id: 2bb407fff6cd1790cf33e8ce5527bb5e44255215
2019-08-19 19:27:30 -07:00
Jun Wu
2da208b718 cliparser: support multiple structures in one define_flags!
Summary: This can make code shorter.

Reviewed By: sfilipco

Differential Revision: D16733264

fbshipit-source-id: 27c0299c6e59bc30cfa14aa9ce122e2a542fd9c1
2019-08-19 19:27:30 -07:00
Jun Wu
7b96c8d292 cliparser: support shell alias
Summary:
Alias starting with `!` are considered shell commands. The entire command
string should be passed roughly as-is to the shell.

The current alias handling uses shlex::split to split the alias into arguments,
then replaces things like `$1` in arguments. The problem is, escaping
shlex::split a complex shell alias, then unesape (shlex::quote) it is not
loseless.

To maintain compatibility for existing complex shell alias configuration,
add a new code path that imitates the Python code behavior.

Reviewed By: sfilipco

Differential Revision: D16814144

fbshipit-source-id: 0e5e95f99bf8b8b16bd8d0cbcadd6760f7f77ebb
2019-08-19 19:27:27 -07:00
Jun Wu
d2e169b097 cliparser: remove ParseOptions::ignore_errors
Summary: It's not actually used anywhere.

Reviewed By: sfilipco

Differential Revision: D16715452

fbshipit-source-id: ea3e1411b38220f7555de11fa71da258c1c9d0d7
2019-08-15 10:08:36 -07:00
Jun Wu
f49868e6e7 cliparser: add a test demostrating incorrect behavior
Summary: `--no-foo` should only work if `foo` is a boolean flag.

Reviewed By: farnz

Differential Revision: D16715454

fbshipit-source-id: 9a8183586aa50fb55f30e19276bd60ebcc23a5fb
2019-08-15 10:08:36 -07:00
Jun Wu
bdd108a854 cliparser: remove create_args in tests
Summary: The helper function is no longer necessary.

Reviewed By: sfilipco

Differential Revision: D16715455

fbshipit-source-id: 3bf528d3c9f18418724793edebf298425a3bba87
2019-08-15 10:08:36 -07:00
Jun Wu
2ee74da41f cliparser: rename ParseOutput::get to pick and make it do the unwrap
Summary:
Previously, `get` returns an `Option<T>`. Almost all callers use `unwrap`
immediately, since they know what flag names are available. Let's just
move the `unwrap` inside the function. `get` in the Rust (and Python)
eco-system is considered nullable and panic-free. Rename it to `pick`
to make the difference more obvious.

I considered using the `Index` trait, but it has to return a reference,
which does not fit into this use-case.

Reviewed By: sfilipco

Differential Revision: D16715453

fbshipit-source-id: f754d208c4a1b0adeddaee82c7db82c790d6436c
2019-08-14 16:58:29 -07:00
Jun Wu
9fbefb5981 cliparser: remove ParseOutput::get_or_default
Summary:
The default value of a flag is already provided with the flag. Therefore
`get_or_default` requiring another `default` does not make much sense.
Remove it.

Reviewed By: farnz

Differential Revision: D16713531

fbshipit-source-id: 95a55289077b7308083b6685f013d1058419a675
2019-08-14 16:58:29 -07:00
Jun Wu
f5cba1eeff cliparser: make ParseOutput::get typed
Summary:
This simplifies a lot of code. For example:

    - let config_path: Vec<String> = result.get("config").unwrap().clone().try_into().unwrap();
    + let config_path: Vec<String> = result.get("config").unwrap();

Also, the use of `TryInto` does not make sense - the code only implements
`Into`, and `TryInto` will panic. Therefore remove `TryInto`.

Reviewed By: farnz

Differential Revision: D16713534

fbshipit-source-id: 29dc71881dd844fa87e086543fb0a3bb5d8837a1
2019-08-14 16:58:28 -07:00
Jun Wu
4a585cf0f3 cliparser: support underscore names in define_flags macro
Summary:
Replace `_` in field names to `-` as long flag name. That is consistent with
the current hg behavior.

Reviewed By: sfilipco, farnz

Differential Revision: D16713530

fbshipit-source-id: ce36caebc6b3131cb418d86ca0fdc507d2d8f17f
2019-08-14 16:58:28 -07:00
Jun Wu
c9b69cdf2a cliparser: support defining short names in define_flags macro
Summary: Add rules to match `#[short('x')]` attributes.

Reviewed By: sfilipco

Differential Revision: D16713537

fbshipit-source-id: eaefd34ef51e64a755920feb402547adf9de96f8
2019-08-14 16:58:28 -07:00
Jun Wu
ca432367ca cliparser: support implicit default in define_flags macro
Summary: Add a rule to match implicit default value.

Reviewed By: sfilipco

Differential Revision: D16713533

fbshipit-source-id: 168fb59ee5607d5ae2c0d848943cbc8f55bdfb82
2019-08-14 16:58:27 -07:00
Jun Wu
11b414bccf cliparser: make define_flags stateful
Summary:
Change the macro to have state so we can parse fields one by one with different
syntax.

Reviewed By: sfilipco

Differential Revision: D16713536

fbshipit-source-id: 7f8cb63cfa0ea000c2c2a431454c2d88d49b9187
2019-08-14 16:58:27 -07:00
Jun Wu
6c3314110c cliparser: add define_flags macro
Summary:
This macro provides `Vec<Flag>` and implements `From<ParseOutput>` for a
struct intended to be used as command options.

The core macro is just 21 lines. Both structopt-derive and gumdrop_derive have
1000+ lines.

Of course, it lacks of features.  Some of those features are:

- No way to provide a "short flag".
- Default values have to be explicitly written.
- No way to compose structs (ex. reuse DiffOpts)
- Should "args" be part of the structure?
- Should "command help" be part of the structure?

Reviewed By: sfilipco

Differential Revision: D16713535

fbshipit-source-id: e8137e5f110ebb280fc40f84dace7b1d3a346c82
2019-08-14 16:58:27 -07:00
Jun Wu
37c696c443 cliparser: stop using vendored shlex
Summary:
The vendored shlex crate was used to unblock progress. Now that `shlex`
is available in tp2 crate, let's drop the vendored shlex.

Reviewed By: farnz

Differential Revision: D16706184

fbshipit-source-id: f110ee91d8c9cc61b94fbafbf269f260bd73fd70
2019-08-08 22:54:08 -07:00
Jun Wu
d6300691a3 hgpython: use ToPyObject trait from cliparser
Summary: This simplifies the code greatly.

Reviewed By: farnz

Differential Revision: D16706189

fbshipit-source-id: d6170d67c386c543e3252cfad28d9ac5277272eb
2019-08-08 22:54:07 -07:00
Jun Wu
90e7f78e28 lib: drop extension-module-2-7 cpython feature
Summary:
`extension-module-2-7` should only be used for "extension modules". They change
link behavior and can cause issues. In our cases, only "python27-sys" is actually
used.

Reviewed By: farnz

Differential Revision: D16706183

fbshipit-source-id: cd2474556f92bb3697a2d209a790baf063195c50
2019-08-08 22:54:07 -07:00
Jun Wu
199ec114ca cliparser: remove command.rs
Summary: This file is not used.

Reviewed By: farnz

Differential Revision: D16703008

fbshipit-source-id: 002c37c011d2c042d9d93b2f7563a0fc0c724e42
2019-08-08 22:54:06 -07:00
Jun Wu
032db283fa cliparser: remove Parser::new
Summary:
Encourage the use of `ParseOptions`. Most users will use
`ParseOptions::parse_args` without using a `Parser`.

Reviewed By: farnz

Differential Revision: D16703010

fbshipit-source-id: 33cc839228dc2d7fe9618c9d83720af2bdc0e1f7
2019-08-08 22:54:06 -07:00
Jun Wu
645d40b4a0 cliparser: move flags state from Parser to ParseOptions
Summary:
This makes `ParseOptions` own all information to do a parse so parsing can be
done without creating a `Parser` struct:

    ParseOptions::new().flags(flags).parse_args(args)

`Parser::with_parsing_options` is removed since that will overwrite `flags`.
Some `unwrap()`s in the bindings are replaced with proper error handling.

In the future it might make sense to make `Parser` or `Parser::new` private.

Reviewed By: farnz

Differential Revision: D16703009

fbshipit-source-id: 6119c29d934ae4410b31d906397a902c03e27fe2
2019-08-08 22:54:06 -07:00
Jun Wu
b6464a5f78 cliparser: remove FlagDefinition
Summary:
`Flag` and `FlagDefinition` serve similar purposes.
Keep `Flag` and drop `FlagDefinition`.

Reviewed By: farnz

Differential Revision: D16700315

fbshipit-source-id: 1b7d1afb1bde01b9781f9d1295a1494053471062
2019-08-08 22:54:05 -07:00
Jun Wu
79886a92e6 clidispatch: remove FlagDefinition use
Summary: Use `Flag::from` or `tuple::into` to build `Flag`s directly.

Reviewed By: farnz

Differential Revision: D16700317

fbshipit-source-id: 9c3545a42fa36f4ec53680e4d3315411ec55aa61
2019-08-08 22:54:05 -07:00
Jun Wu
879b4eb382 cliparser: remove Flag::new use in tests
Summary: Migrate them to use `Flag::from` or `tuple::into` to build `Flag`s directly.

Reviewed By: farnz

Differential Revision: D16700692

fbshipit-source-id: e5a7ce3b937f482149c9a84e4e1ab1a8807dfe4c
2019-08-08 22:54:04 -07:00
Jun Wu
aaa9fdb0a0 cliparser: remove FlagDefinition use in tests
Summary: Migrate them to use `Flag::from` or `tuple::into` to build `Flag`s directly.

Reviewed By: farnz

Differential Revision: D16700305

fbshipit-source-id: 4511607360e1a2ceb7accbb2cb745bf1069c2920
2019-08-08 22:54:04 -07:00
Jun Wu
95bdf77f67 cliparser: move hgflags to clidispatch
Summary:
The "global flags" concept is more coupled with the "hg dispatch" logic.
The "cliparser" crate can stay pure from application-specific flags.

As we're here, update the global flags definition to use `Into::into`,
and return `Vec<Flag>` directly, instead of `Vec<FlagDefinition>`.
That removes one usecase of `FlagDefinition`.

Reviewed By: farnz

Differential Revision: D16700307

fbshipit-source-id: 2688cb06272a249454d92d7d7d5a0b1100805dbd
2019-08-08 22:54:04 -07:00
Jun Wu
2542ab6b62 cliparser: implement more conversions for Flag
Summary:
The goal is to simplify the API, keep only the `Flag` structure, and remove
`FlagDefinition`.

Type parameters is used to make the API more flexibile to use.

Reviewed By: farnz

Differential Revision: D16700311

fbshipit-source-id: cab894ca21679540e789dbbfe4958facab0d7bbd
2019-08-08 22:54:03 -07:00
Jun Wu
8c3621bb5a cliparser: rename Flag::value_type to Flag::default_value
Summary: The field not only decides the type, but also provides the default.

Reviewed By: farnz

Differential Revision: D16700308

fbshipit-source-id: 73aec2c227c7ce6f5b19aa5ae093f37f0b265cd3
2019-08-08 22:54:03 -07:00
Jun Wu
55192961eb cliparser: remove lifetime from types
Summary: Remove lifetime from `FlagDefinition`, `Flag`, and `Parser`.

Reviewed By: farnz

Differential Revision: D16700312

fbshipit-source-id: 4789015ff3f0ef4bb52326b5f3191ca04859151c
2019-08-08 22:54:03 -07:00
Jun Wu
57da9728ba cliparser: make Parser::new consume Vec<Flag>
Summary:
This helps removing references. The internal structure of `Parser` is changed a
bit to avoid storing `Flag` copies.

Reviewed By: farnz

Differential Revision: D16700316

fbshipit-source-id: 01d66feb8bd5cc2fa9394b3635f97e098143597d
2019-08-08 22:54:03 -07:00
Jun Wu
6337269932 cliparser: use char instead of &char in Parser
Summary: `char` is only 4 bytes. Using the concrete type is faster than a reference.

Reviewed By: farnz

Differential Revision: D16700306

fbshipit-source-id: d7f198238005ec5a0061ed79724b795cd1e5077b
2019-08-08 22:54:02 -07:00
Jun Wu
5f2f7fa749 cliparser: make Flag::new consume FlagDefinition
Summary:
This is a step towards:

- Getting rid of `<'a>` lifetime from `Flag` and `FlagDefinition`.
- Getting rid of `FlagDefinition` - it's too similar to `Flag` and is not
  really a dedicated type (it's a type alias).

Reviewed By: farnz

Differential Revision: D16700313

fbshipit-source-id: b517d5203bcd1eff3381c7c6ae1020965bd135b0
2019-08-08 22:54:02 -07:00
Jun Wu
5670dee694 cliparser: make Flag.description non-optional
Summary:
It does not make much sense for a flag to not have a description, and there are
no users depending on that.  Therefore drop the `Option`.

Reviewed By: farnz

Differential Revision: D16700318

fbshipit-source-id: 4566cf339088ba69b64dd8a316a9f36fb1fac81b
2019-08-08 22:54:02 -07:00
Jun Wu
3cce1b3c2f cliparser: merge _parse into parse_args
Summary:
Leading underscores are uncommon in Rust. Since `parse_args` just calls `_parse`,
merge `_parse` into `parse_args`.

Input types of `parse_args` are changed a bit to be more flexible.

Reviewed By: farnz

Differential Revision: D16700314

fbshipit-source-id: 6e13ca7796cbb535fef79e3a723002bf7ef8d6f6
2019-08-08 22:54:02 -07:00
Jun Wu
f8fdd156a2 cliparser: rename OpenOptions to ParseOptions
Summary: The options decides how the parser works. It is not opening anything.

Reviewed By: farnz

Differential Revision: D16700310

fbshipit-source-id: c0ab69f9d8c50664dc157ef0eb0f12ee1533e81d
2019-08-08 22:54:01 -07:00
Jun Wu
28a5be0d2c cliparser: fix Cargo.toml
Summary:
This removes the warning:

  warning: cliparser/Cargo.toml: `default-features = [".."]` was found in [features]. Did you mean to use `default = [".."]`?

Reviewed By: sfilipco

Differential Revision: D16672732

fbshipit-source-id: 2c7478c54f0302113d4e065f88631c096757e829
2019-08-06 15:52:27 -07:00
Jorge Lopez Silva
a1fd50cc9b Upgrade smc/hg to cpython/python-sys 0.3
Summary: Bump cpython and python27-sys to 0.3.

Reviewed By: quark-zju

Differential Revision: D16634957

fbshipit-source-id: 9f6b45baf8098399109ad03bf440da42f4483fcb
2019-08-05 09:56:45 -07:00
Jun Wu
bd75a8e099 cliparser: fix regression on error message
Summary:
Fix the regression caused by D16557266 on error message. The fix is not the
most efficient, though. But it should restore the behavior.

Reviewed By: singhsrb

Differential Revision: D16557654

fbshipit-source-id: 2eb6956f99c931f94201b875c8bebb2219a794f0
2019-07-30 08:43:39 -07:00
Jun Wu
5c24ff642e cliparser: special handle debug commands
Summary:
When doing command name prefix match, if there are a unique match for non-debug
commands, use it. This solves `hg d` regression.

Note: we cannot simply use `starts_with("debug")` to test debug commands. That
is because debug commands can have aliases (ex. `debug|dbsh` as the command
name definition, and `dbsh` is also a debug command that does not starts with
`debug`). Therefore it gets a bit complex.

Reviewed By: kulshrax

Differential Revision: D16557266

fbshipit-source-id: 42d814940c9217d9e554bd0fe2769a53e0ee467f
2019-07-29 19:35:32 -07:00
Jun Wu
cad6e0410b cliparser: rename IllformedAlias to MalformedAlias
Summary: Suggested by kulshrax, `I` and `l` are harder to distinugish. Rename it to clarify.

Reviewed By: kulshrax

Differential Revision: D16556868

fbshipit-source-id: 5892f0730f5205534851c5f7c024ff98f751423d
2019-07-29 19:35:32 -07:00
Jun Wu
8a043e1f34 cliparser: add more test cases about alias handling
Summary: This just makes the code slightly more confident.

Reviewed By: kulshrax

Differential Revision: D16556871

fbshipit-source-id: 5304c3e60c80242974d35a53aace84f829418bb8
2019-07-29 19:35:31 -07:00
Jun Wu
52a45a0632 cliparser: record the index of first positional argument in parse output
Summary: This will be used to accurately replace the command line arguments.

Reviewed By: kulshrax

Differential Revision: D16556870

fbshipit-source-id: efb77a0da17c436e0123354746ba85201813992a
2019-07-29 19:35:31 -07:00
Jun Wu
b434779e81 cliparser: support expanding $1 in aliases
Summary: Expand `$1`, `$2` etc. in aliases. There are users depending on this behavior.

Reviewed By: kulshrax

Differential Revision: D16531016

fbshipit-source-id: fed735339adc82c4f52b74c4c4d62818bc047809
2019-07-29 19:35:30 -07:00
Jun Wu
94073b6891 cliparser: make expand_aliases take a lookup function
Summary:
This avoids calculating the full alias map, and provides more flexibility (ex.
the function can take care of `defaults` handling).

Reviewed By: kulshrax

Differential Revision: D16530514

fbshipit-source-id: 63558da5c9af36b2f8bc7689174fed71593a8186
2019-07-29 19:35:29 -07:00