1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 12:21:48 +02:00

Merge pull request #13008 from Mic92/aliases

Move alias support from NixArgs to MultiCommand + test
This commit is contained in:
Jörg Thalheim 2025-04-12 11:06:09 +02:00 committed by GitHub
commit 71567373b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 78 additions and 73 deletions

View file

@ -647,4 +647,25 @@ nlohmann::json MultiCommand::toJSON()
return res;
}
Strings::iterator MultiCommand::rewriteArgs(Strings & args, Strings::iterator pos)
{
if (command)
return command->second->rewriteArgs(args, pos);
if (aliasUsed || pos == args.end()) return pos;
auto arg = *pos;
auto i = aliases.find(arg);
if (i == aliases.end()) return pos;
auto & info = i->second;
if (info.status == AliasStatus::Deprecated) {
warn("'%s' is a deprecated alias for '%s'",
arg, concatStringsSep(" ", info.replacement));
}
pos = args.erase(pos);
for (auto j = info.replacement.rbegin(); j != info.replacement.rend(); ++j)
pos = args.insert(pos, *j);
aliasUsed = true;
return pos;
}
}

View file

@ -393,8 +393,30 @@ public:
nlohmann::json toJSON() override;
enum struct AliasStatus {
/** Aliases that don't go away */
AcceptedShorthand,
/** Aliases that will go away */
Deprecated,
};
/** An alias, except for the original syntax, which is in the map key. */
struct AliasInfo {
AliasStatus status;
std::vector<std::string> replacement;
};
/**
* A list of aliases (remapping a deprecated/shorthand subcommand
* to something else).
*/
std::map<std::string, AliasInfo> aliases;
Strings::iterator rewriteArgs(Strings & args, Strings::iterator pos) override;
protected:
std::string commandName = "";
bool aliasUsed = false;
};
Strings argvToStrings(int argc, char * * argv);