2016-02-09 22:07:48 +02:00
|
|
|
|
#pragma once
|
2023-04-01 06:18:41 +03:00
|
|
|
|
///@file
|
2016-02-09 22:07:48 +02:00
|
|
|
|
|
|
|
|
|
#include <iostream>
|
2023-10-25 07:43:36 +03:00
|
|
|
|
#include <functional>
|
2016-02-09 22:07:48 +02:00
|
|
|
|
#include <map>
|
|
|
|
|
#include <memory>
|
2023-10-25 07:43:36 +03:00
|
|
|
|
#include <optional>
|
2016-02-09 22:07:48 +02:00
|
|
|
|
|
2020-08-17 18:44:52 +03:00
|
|
|
|
#include <nlohmann/json_fwd.hpp>
|
|
|
|
|
|
2023-10-25 07:43:36 +03:00
|
|
|
|
#include "types.hh"
|
|
|
|
|
#include "experimental-features.hh"
|
2016-02-09 22:07:48 +02:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
2023-11-28 15:20:27 +02:00
|
|
|
|
enum struct HashAlgorithm : char;
|
2023-11-28 16:38:15 +02:00
|
|
|
|
enum struct HashFormat : int;
|
2016-02-09 22:07:48 +02:00
|
|
|
|
|
2021-09-13 15:41:28 +03:00
|
|
|
|
class MultiCommand;
|
|
|
|
|
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
class RootArgs;
|
|
|
|
|
|
|
|
|
|
class AddCompletions;
|
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
class Args
|
|
|
|
|
{
|
2023-05-12 20:54:54 +03:00
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
public:
|
|
|
|
|
|
2021-08-28 23:26:53 +03:00
|
|
|
|
/**
|
|
|
|
|
* Return a short one-line description of the command.
|
|
|
|
|
*/
|
2016-02-09 22:07:48 +02:00
|
|
|
|
virtual std::string description() { return ""; }
|
|
|
|
|
|
2022-01-31 19:03:24 +02:00
|
|
|
|
virtual bool forceImpureByDefault() { return false; }
|
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Return documentation about this command, in Markdown format.
|
|
|
|
|
*/
|
2020-12-07 14:04:24 +02:00
|
|
|
|
virtual std::string doc() { return ""; }
|
|
|
|
|
|
2023-05-12 20:54:54 +03:00
|
|
|
|
/**
|
|
|
|
|
* @brief Get the base directory for the command.
|
|
|
|
|
*
|
|
|
|
|
* @return Generally the working directory, but in case of a shebang
|
|
|
|
|
* interpreter, returns the directory of the script.
|
|
|
|
|
*
|
|
|
|
|
* This only returns the correct value after parseCmdline() has run.
|
|
|
|
|
*/
|
2023-10-23 19:38:54 +03:00
|
|
|
|
virtual Path getCommandBaseDir() const;
|
2023-05-12 20:54:54 +03:00
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
protected:
|
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* The largest `size_t` is used to indicate the "any" arity, for
|
|
|
|
|
* handlers/flags/arguments that accept an arbitrary number of
|
|
|
|
|
* arguments.
|
|
|
|
|
*/
|
2017-08-29 15:28:57 +03:00
|
|
|
|
static const size_t ArityAny = std::numeric_limits<size_t>::max();
|
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Arguments (flags/options and positional) have a "handler" which is
|
|
|
|
|
* caused when the argument is parsed. The handler has an arbitrary side
|
|
|
|
|
* effect, including possible affect further command-line parsing.
|
|
|
|
|
*
|
|
|
|
|
* There are many constructors in order to support many shorthand
|
|
|
|
|
* initializations, and this is used a lot.
|
|
|
|
|
*/
|
2020-05-11 16:46:18 +03:00
|
|
|
|
struct Handler
|
|
|
|
|
{
|
|
|
|
|
std::function<void(std::vector<std::string>)> fun;
|
|
|
|
|
size_t arity;
|
|
|
|
|
|
2023-10-23 16:56:30 +03:00
|
|
|
|
Handler() = default;
|
2020-05-11 16:46:18 +03:00
|
|
|
|
|
|
|
|
|
Handler(std::function<void(std::vector<std::string>)> && fun)
|
|
|
|
|
: fun(std::move(fun))
|
|
|
|
|
, arity(ArityAny)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
Handler(std::function<void()> && handler)
|
|
|
|
|
: fun([handler{std::move(handler)}](std::vector<std::string>) { handler(); })
|
|
|
|
|
, arity(0)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
Handler(std::function<void(std::string)> && handler)
|
|
|
|
|
: fun([handler{std::move(handler)}](std::vector<std::string> ss) {
|
|
|
|
|
handler(std::move(ss[0]));
|
|
|
|
|
})
|
|
|
|
|
, arity(1)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
Handler(std::function<void(std::string, std::string)> && handler)
|
|
|
|
|
: fun([handler{std::move(handler)}](std::vector<std::string> ss) {
|
|
|
|
|
handler(std::move(ss[0]), std::move(ss[1]));
|
|
|
|
|
})
|
|
|
|
|
, arity(2)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
Handler(std::vector<std::string> * dest)
|
2023-10-23 18:06:15 +03:00
|
|
|
|
: fun([dest](std::vector<std::string> ss) { *dest = ss; })
|
2020-05-11 16:46:18 +03:00
|
|
|
|
, arity(ArityAny)
|
|
|
|
|
{ }
|
|
|
|
|
|
2021-01-08 11:44:55 +02:00
|
|
|
|
Handler(std::string * dest)
|
2023-10-23 18:06:15 +03:00
|
|
|
|
: fun([dest](std::vector<std::string> ss) { *dest = ss[0]; })
|
2021-01-08 11:44:55 +02:00
|
|
|
|
, arity(1)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
Handler(std::optional<std::string> * dest)
|
2023-10-23 18:06:15 +03:00
|
|
|
|
: fun([dest](std::vector<std::string> ss) { *dest = ss[0]; })
|
2020-05-11 16:46:18 +03:00
|
|
|
|
, arity(1)
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
Handler(T * dest, const T & val)
|
2023-10-23 18:06:15 +03:00
|
|
|
|
: fun([dest, val](std::vector<std::string> ss) { *dest = val; })
|
2020-05-11 16:46:18 +03:00
|
|
|
|
, arity(0)
|
|
|
|
|
{ }
|
2021-01-08 11:44:55 +02:00
|
|
|
|
|
|
|
|
|
template<class I>
|
|
|
|
|
Handler(I * dest)
|
2023-10-23 18:06:15 +03:00
|
|
|
|
: fun([dest](std::vector<std::string> ss) {
|
2021-01-08 13:51:19 +02:00
|
|
|
|
*dest = string2IntWithUnitPrefix<I>(ss[0]);
|
2021-01-08 11:44:55 +02:00
|
|
|
|
})
|
|
|
|
|
, arity(1)
|
|
|
|
|
{ }
|
2021-09-14 20:05:28 +03:00
|
|
|
|
|
|
|
|
|
template<class I>
|
|
|
|
|
Handler(std::optional<I> * dest)
|
2023-10-23 18:06:15 +03:00
|
|
|
|
: fun([dest](std::vector<std::string> ss) {
|
2021-09-14 20:05:28 +03:00
|
|
|
|
*dest = string2IntWithUnitPrefix<I>(ss[0]);
|
|
|
|
|
})
|
|
|
|
|
, arity(1)
|
|
|
|
|
{ }
|
2020-05-11 16:46:18 +03:00
|
|
|
|
};
|
|
|
|
|
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
/**
|
|
|
|
|
* The basic function type of the completion callback.
|
|
|
|
|
*
|
|
|
|
|
* Used to define `CompleterClosure` and some common case completers
|
|
|
|
|
* that individual flags/arguments can use.
|
|
|
|
|
*
|
|
|
|
|
* The `AddCompletions` that is passed is an interface to the state
|
|
|
|
|
* stored as part of the root command
|
|
|
|
|
*/
|
2023-10-23 18:07:17 +03:00
|
|
|
|
using CompleterFun = void(AddCompletions &, size_t, std::string_view);
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The closure type of the completion callback.
|
|
|
|
|
*
|
|
|
|
|
* This is what is actually stored as part of each Flag / Expected
|
|
|
|
|
* Arg.
|
|
|
|
|
*/
|
2023-10-23 18:07:17 +03:00
|
|
|
|
using CompleterClosure = std::function<CompleterFun>;
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Description of flags / options
|
|
|
|
|
*
|
|
|
|
|
* These are arguments like `-s` or `--long` that can (mostly)
|
|
|
|
|
* appear in any order.
|
|
|
|
|
*/
|
2016-02-09 22:07:48 +02:00
|
|
|
|
struct Flag
|
|
|
|
|
{
|
2023-10-23 18:07:17 +03:00
|
|
|
|
using ptr = std::shared_ptr<Flag>;
|
2020-05-04 23:40:19 +03:00
|
|
|
|
|
2017-06-07 19:41:20 +03:00
|
|
|
|
std::string longName;
|
2021-02-07 21:44:56 +02:00
|
|
|
|
std::set<std::string> aliases;
|
2017-06-07 19:41:20 +03:00
|
|
|
|
char shortName = 0;
|
2016-02-09 22:07:48 +02:00
|
|
|
|
std::string description;
|
2017-06-07 19:41:20 +03:00
|
|
|
|
std::string category;
|
2020-05-04 23:40:19 +03:00
|
|
|
|
Strings labels;
|
|
|
|
|
Handler handler;
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
CompleterClosure completer;
|
2020-05-04 23:40:19 +03:00
|
|
|
|
|
2023-01-17 02:13:31 +02:00
|
|
|
|
std::optional<ExperimentalFeature> experimentalFeature;
|
|
|
|
|
|
2023-11-28 15:41:44 +02:00
|
|
|
|
static Flag mkHashAlgoFlag(std::string && longName, HashAlgorithm * ha);
|
|
|
|
|
static Flag mkHashAlgoOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha);
|
2023-11-28 16:38:15 +02:00
|
|
|
|
static Flag mkHashFormatFlagWithDefault(std::string && longName, HashFormat * hf);
|
|
|
|
|
static Flag mkHashFormatOptFlag(std::string && longName, std::optional<HashFormat> * ohf);
|
2016-02-09 22:07:48 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Index of all registered "long" flag descriptions (flags like
|
|
|
|
|
* `--long`).
|
|
|
|
|
*/
|
2017-06-07 19:41:20 +03:00
|
|
|
|
std::map<std::string, Flag::ptr> longFlags;
|
2023-10-16 17:42:15 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Index of all registered "short" flag descriptions (flags like
|
|
|
|
|
* `-s`).
|
|
|
|
|
*/
|
2017-06-07 19:41:20 +03:00
|
|
|
|
std::map<char, Flag::ptr> shortFlags;
|
2016-02-09 22:07:48 +02:00
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Process a single flag and its arguments, pulling from an iterator
|
|
|
|
|
* of raw CLI args as needed.
|
|
|
|
|
*/
|
2016-02-09 22:07:48 +02:00
|
|
|
|
virtual bool processFlag(Strings::iterator & pos, Strings::iterator end);
|
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Description of positional arguments
|
|
|
|
|
*
|
|
|
|
|
* These are arguments that do not start with a `-`, and for which
|
|
|
|
|
* the order does matter.
|
|
|
|
|
*/
|
2016-02-09 22:07:48 +02:00
|
|
|
|
struct ExpectedArg
|
|
|
|
|
{
|
|
|
|
|
std::string label;
|
2020-05-10 22:35:07 +03:00
|
|
|
|
bool optional = false;
|
2020-05-11 16:46:18 +03:00
|
|
|
|
Handler handler;
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
CompleterClosure completer;
|
2016-02-09 22:07:48 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Queue of expected positional argument forms.
|
|
|
|
|
*
|
2023-10-24 23:22:05 +03:00
|
|
|
|
* Positional argument descriptions are inserted on the back.
|
2023-10-16 17:42:15 +03:00
|
|
|
|
*
|
|
|
|
|
* As positional arguments are passed, these are popped from the
|
|
|
|
|
* front, until there are hopefully none left as all args that were
|
|
|
|
|
* expected in fact were passed.
|
|
|
|
|
*/
|
2016-02-09 22:07:48 +02:00
|
|
|
|
std::list<ExpectedArg> expectedArgs;
|
2023-10-24 23:22:05 +03:00
|
|
|
|
/**
|
|
|
|
|
* List of processed positional argument forms.
|
2023-12-06 15:13:45 +02:00
|
|
|
|
*
|
2023-10-24 23:22:05 +03:00
|
|
|
|
* All items removed from `expectedArgs` are added here. After all
|
|
|
|
|
* arguments were processed, this list should be exactly the same as
|
|
|
|
|
* `expectedArgs` was before.
|
2023-12-06 15:13:45 +02:00
|
|
|
|
*
|
2023-10-24 23:22:05 +03:00
|
|
|
|
* This list is used to extend the lifetime of the argument forms.
|
|
|
|
|
* If this is not done, some closures that reference the command
|
|
|
|
|
* itself will segfault.
|
|
|
|
|
*/
|
|
|
|
|
std::list<ExpectedArg> processedArgs;
|
2016-02-09 22:07:48 +02:00
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* Process some positional arugments
|
|
|
|
|
*
|
|
|
|
|
* @param finish: We have parsed everything else, and these are the only
|
|
|
|
|
* arguments left. Used because we accumulate some "pending args" we might
|
|
|
|
|
* have left over.
|
|
|
|
|
*/
|
2016-02-09 22:07:48 +02:00
|
|
|
|
virtual bool processArgs(const Strings & args, bool finish);
|
|
|
|
|
|
2020-12-03 23:45:44 +02:00
|
|
|
|
virtual Strings::iterator rewriteArgs(Strings & args, Strings::iterator pos)
|
|
|
|
|
{ return pos; }
|
|
|
|
|
|
2017-06-07 19:41:20 +03:00
|
|
|
|
std::set<std::string> hiddenCategories;
|
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Called after all command line flags before the first non-flag
|
|
|
|
|
* argument (if any) have been processed.
|
|
|
|
|
*/
|
2021-01-28 16:37:43 +02:00
|
|
|
|
virtual void initialFlagsProcessed() {}
|
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
public:
|
|
|
|
|
|
2020-05-04 23:40:19 +03:00
|
|
|
|
void addFlag(Flag && flag);
|
2017-06-07 19:41:20 +03:00
|
|
|
|
|
2021-02-26 15:55:54 +02:00
|
|
|
|
void removeFlag(const std::string & longName);
|
|
|
|
|
|
2020-05-11 16:46:18 +03:00
|
|
|
|
void expectArgs(ExpectedArg && arg)
|
|
|
|
|
{
|
|
|
|
|
expectedArgs.emplace_back(std::move(arg));
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Expect a string argument.
|
|
|
|
|
*/
|
2022-02-25 17:00:00 +02:00
|
|
|
|
void expectArg(const std::string & label, std::string * dest, bool optional = false)
|
2016-02-09 22:07:48 +02:00
|
|
|
|
{
|
2020-05-11 16:46:18 +03:00
|
|
|
|
expectArgs({
|
|
|
|
|
.label = label,
|
2020-09-25 11:27:40 +03:00
|
|
|
|
.optional = optional,
|
2020-05-11 16:46:18 +03:00
|
|
|
|
.handler = {dest}
|
|
|
|
|
});
|
2016-02-09 22:07:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Expect 0 or more arguments.
|
|
|
|
|
*/
|
2017-10-24 13:45:11 +03:00
|
|
|
|
void expectArgs(const std::string & label, std::vector<std::string> * dest)
|
2016-02-09 22:07:48 +02:00
|
|
|
|
{
|
2020-05-11 16:46:18 +03:00
|
|
|
|
expectArgs({
|
|
|
|
|
.label = label,
|
|
|
|
|
.handler = {dest}
|
|
|
|
|
});
|
2016-02-09 22:07:48 +02:00
|
|
|
|
}
|
2016-02-09 22:28:29 +02:00
|
|
|
|
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
static CompleterFun completePath;
|
|
|
|
|
|
|
|
|
|
static CompleterFun completeDir;
|
|
|
|
|
|
2020-08-17 18:44:52 +03:00
|
|
|
|
virtual nlohmann::json toJSON();
|
|
|
|
|
|
2016-02-09 22:28:29 +02:00
|
|
|
|
friend class MultiCommand;
|
2021-09-13 15:41:28 +03:00
|
|
|
|
|
2023-10-16 17:42:15 +03:00
|
|
|
|
/**
|
|
|
|
|
* The parent command, used if this is a subcommand.
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
*
|
|
|
|
|
* Invariant: An Args with a null parent must also be a RootArgs
|
|
|
|
|
*
|
|
|
|
|
* \todo this would probably be better in the CommandClass.
|
|
|
|
|
* getRoot() could be an abstract method that peels off at most one
|
|
|
|
|
* layer before recuring.
|
2023-10-16 17:42:15 +03:00
|
|
|
|
*/
|
2021-09-13 15:41:28 +03:00
|
|
|
|
MultiCommand * parent = nullptr;
|
2023-01-17 02:13:31 +02:00
|
|
|
|
|
|
|
|
|
/**
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
* Traverse parent pointers until we find the \ref RootArgs "root
|
|
|
|
|
* arguments" object.
|
2023-01-17 02:13:31 +02:00
|
|
|
|
*/
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
RootArgs & getRoot();
|
2016-02-09 22:07:48 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* A command is an argument parser that can be executed by calling its
|
|
|
|
|
* run() method.
|
|
|
|
|
*/
|
2021-09-13 15:41:28 +03:00
|
|
|
|
struct Command : virtual public Args
|
2018-11-22 17:03:31 +02:00
|
|
|
|
{
|
2019-06-20 00:37:40 +03:00
|
|
|
|
friend class MultiCommand;
|
|
|
|
|
|
2023-10-23 18:07:57 +03:00
|
|
|
|
virtual ~Command() = default;
|
2019-06-18 17:01:35 +03:00
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Entry point to the command
|
|
|
|
|
*/
|
2018-11-22 17:03:31 +02:00
|
|
|
|
virtual void run() = 0;
|
|
|
|
|
|
2023-10-23 18:07:17 +03:00
|
|
|
|
using Category = int;
|
2020-05-05 16:18:23 +03:00
|
|
|
|
|
|
|
|
|
static constexpr Category catDefault = 0;
|
|
|
|
|
|
2023-09-29 04:21:58 +03:00
|
|
|
|
virtual std::optional<ExperimentalFeature> experimentalFeature();
|
2023-01-17 06:01:18 +02:00
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
|
virtual Category category() { return catDefault; }
|
2018-11-22 17:03:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2023-10-23 18:07:17 +03:00
|
|
|
|
using Commands = std::map<std::string, std::function<ref<Command>()>>;
|
2018-11-22 17:03:31 +02:00
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* An argument parser that supports multiple subcommands,
|
|
|
|
|
* i.e. ‘<command> <subcommand>’.
|
|
|
|
|
*/
|
2021-09-13 15:41:28 +03:00
|
|
|
|
class MultiCommand : virtual public Args
|
2018-11-22 17:03:31 +02:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Commands commands;
|
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
|
std::map<Command::Category, std::string> categories;
|
|
|
|
|
|
2023-03-27 04:12:25 +03:00
|
|
|
|
/**
|
|
|
|
|
* Selected command, if any.
|
|
|
|
|
*/
|
2020-05-05 16:18:23 +03:00
|
|
|
|
std::optional<std::pair<std::string, ref<Command>>> command;
|
2018-11-22 17:03:31 +02:00
|
|
|
|
|
2023-12-06 15:13:45 +02:00
|
|
|
|
MultiCommand(std::string_view commandName, const Commands & commands);
|
2018-11-22 17:03:31 +02:00
|
|
|
|
|
|
|
|
|
bool processFlag(Strings::iterator & pos, Strings::iterator end) override;
|
|
|
|
|
|
|
|
|
|
bool processArgs(const Strings & args, bool finish) override;
|
2020-08-17 18:44:52 +03:00
|
|
|
|
|
|
|
|
|
nlohmann::json toJSON() override;
|
2023-12-06 15:13:45 +02:00
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::string commandName = "";
|
2018-11-22 17:03:31 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
Strings argvToStrings(int argc, char * * argv);
|
|
|
|
|
|
2020-10-09 10:39:51 +03:00
|
|
|
|
struct Completion {
|
|
|
|
|
std::string completion;
|
|
|
|
|
std::string description;
|
|
|
|
|
|
|
|
|
|
bool operator<(const Completion & other) const;
|
|
|
|
|
};
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The abstract interface for completions callbacks
|
|
|
|
|
*
|
|
|
|
|
* The idea is to restrict the callback so it can only add additional
|
|
|
|
|
* completions to the collection, or set the completion type. By making
|
|
|
|
|
* it go through this interface, the callback cannot make any other
|
|
|
|
|
* changes, or even view the completions / completion type that have
|
|
|
|
|
* been set so far.
|
|
|
|
|
*/
|
|
|
|
|
class AddCompletions
|
|
|
|
|
{
|
2020-10-09 10:39:51 +03:00
|
|
|
|
public:
|
2021-12-22 13:37:59 +02:00
|
|
|
|
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
/**
|
|
|
|
|
* The type of completion we are collecting.
|
|
|
|
|
*/
|
|
|
|
|
enum class Type {
|
|
|
|
|
Normal,
|
|
|
|
|
Filenames,
|
|
|
|
|
Attrs,
|
|
|
|
|
};
|
2020-05-10 21:32:21 +03:00
|
|
|
|
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
/**
|
|
|
|
|
* Set the type of the completions being collected
|
|
|
|
|
*
|
|
|
|
|
* \todo it should not be possible to change the type after it has been set.
|
|
|
|
|
*/
|
|
|
|
|
virtual void setType(Type type) = 0;
|
2020-05-10 22:35:07 +03:00
|
|
|
|
|
Overhaul completions, redo #6693 (#8131)
As I complained in
https://github.com/NixOS/nix/pull/6784#issuecomment-1421777030 (a
comment on the wrong PR, sorry again!), #6693 introduced a second
completions mechanism to fix a bug. Having two completion mechanisms
isn't so nice.
As @thufschmitt also pointed out, it was a bummer to go from `FlakeRef`
to `std::string` when collecting flake refs. Now it is `FlakeRefs`
again.
The underlying issue that sought to work around was that completion of
arguments not at the end can still benefit from the information from
latter arguments.
To fix this better, we rip out that change and simply defer all
completion processing until after all the (regular, already-complete)
arguments have been passed.
In addition, I noticed the original completion logic used some global
variables. I do not like global variables, because even if they save
lines of code, they also obfuscate the architecture of the code.
I got rid of them moved them to a new `RootArgs` class, which now has
`parseCmdline` instead of `Args`. The idea is that we have many argument
parsers from subcommands and what-not, but only one root args that owns
the other per actual parsing invocation. The state that was global is
now part of the root args instead.
This did, admittedly, add a bunch of new code. And I do feel bad about
that. So I went and added a lot of API docs to try to at least make the
current state of things clear to the next person.
--
This is needed for RFC 134 (tracking issue #7868). It was very hard to
modularize `Installable` parsing when there were two completion
arguments. I wouldn't go as far as to say it is *easy* now, but at least
it is less hard (and the completions test finally passed).
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-10-23 16:03:11 +03:00
|
|
|
|
/**
|
|
|
|
|
* Add a single completion to the collection
|
|
|
|
|
*/
|
|
|
|
|
virtual void add(std::string completion, std::string description = "") = 0;
|
|
|
|
|
};
|
2020-05-11 23:04:13 +03:00
|
|
|
|
|
2023-11-06 19:19:14 +02:00
|
|
|
|
Strings parseShebangContent(std::string_view s);
|
|
|
|
|
|
2016-02-09 22:07:48 +02:00
|
|
|
|
}
|