diff options
author | Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com> | 2022-07-12 09:12:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-12 09:12:00 +0200 |
commit | f071eb3702c7aa86fc1885321d73a3707ed702b6 (patch) | |
tree | 6ebb56e21b2f514348971bffd25250e3b1712e86 /src/libutil | |
parent | 7e23039b7f491f8517309e0c20653d6d80c37dd7 (diff) | |
parent | 711b2e1f48316d80853635408c518e3562a1fa37 (diff) |
Merge pull request #6693 from ncfavier/complete-flake-inputs
Improve shell completion of flake inputs
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/args.cc | 10 | ||||
-rw-r--r-- | src/libutil/args.hh | 7 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 4b8c55686..44b63f0f6 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -124,7 +124,7 @@ bool Args::processFlag(Strings::iterator & pos, Strings::iterator end) bool anyCompleted = false; for (size_t n = 0 ; n < flag.handler.arity; ++n) { if (pos == end) { - if (flag.handler.arity == ArityAny) break; + if (flag.handler.arity == ArityAny || anyCompleted) break; throw UsageError("flag '%s' requires %d argument(s)", name, flag.handler.arity); } if (auto prefix = needsCompletion(*pos)) { @@ -362,6 +362,14 @@ bool MultiCommand::processArgs(const Strings & args, bool finish) return Args::processArgs(args, finish); } +void MultiCommand::completionHook() +{ + if (command) + return command->second->completionHook(); + else + return Args::completionHook(); +} + nlohmann::json MultiCommand::toJSON() { auto cmds = nlohmann::json::object(); diff --git a/src/libutil/args.hh b/src/libutil/args.hh index 07c017719..84866f12b 100644 --- a/src/libutil/args.hh +++ b/src/libutil/args.hh @@ -148,6 +148,11 @@ protected: argument (if any) have been processed. */ virtual void initialFlagsProcessed() {} + /* Called after the command line has been processed if we need to generate + completions. Useful for commands that need to know the whole command line + in order to know what completions to generate. */ + virtual void completionHook() { } + public: void addFlag(Flag && flag); @@ -223,6 +228,8 @@ public: bool processArgs(const Strings & args, bool finish) override; + void completionHook() override; + nlohmann::json toJSON() override; }; |