Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
nix::Args Class Reference
Inheritance diagram for nix::Args:
CmdFlakeInitCommon FlakeCommand MixCat MixLs MixProfileElementMatchers RegistryCommand nix::Command nix::InstallableCommand nix::MixCommonArgs nix::MixDryRun nix::MixEnvironment nix::MixEvalArgs nix::MixFlakeOptions nix::MixJSON nix::MixOperateOnOptions nix::MixReadOnlyOption nix::MixRepair nix::MultiCommand nix::RawInstallablesCommand nix::RootArgs nix::SourceExprCommand

Classes

struct  ExpectedArg
 
struct  Flag
 
struct  Handler
 

Public Member Functions

virtual std::string description ()
 
virtual bool forceImpureByDefault ()
 
virtual std::string doc ()
 
virtual Path getCommandBaseDir () const
 Get the base directory for the command.
 
void addFlag (Flag &&flag)
 
void removeFlag (const std::string &longName)
 
void expectArgs (ExpectedArg &&arg)
 
void expectArg (const std::string &label, std::string *dest, bool optional=false)
 
void expectArg (const std::string &label, std::filesystem::path *dest, bool optional=false)
 
void expectArgs (const std::string &label, std::vector< std::string > *dest)
 
virtual nlohmann::json toJSON ()
 
RootArgsgetRoot ()
 

Public Attributes

MultiCommandparent = nullptr
 

Static Public Attributes

static CompleterFun completePath
 
static CompleterFun completeDir
 

Protected Types

using CompleterFun = void(AddCompletions &, size_t, std::string_view)
 
using CompleterClosure = std::function<CompleterFun>
 

Protected Member Functions

virtual bool processFlag (Strings::iterator &pos, Strings::iterator end)
 
virtual bool processArgs (const Strings &args, bool finish)
 
virtual Strings::iterator rewriteArgs (Strings &args, Strings::iterator pos)
 
virtual void initialFlagsProcessed ()
 

Protected Attributes

std::map< std::string, Flag::ptr > longFlags
 
std::map< char, Flag::ptr > shortFlags
 
std::list< ExpectedArgexpectedArgs
 
std::list< ExpectedArgprocessedArgs
 
std::set< std::string > hiddenCategories
 

Static Protected Attributes

static const size_t ArityAny = std::numeric_limits<size_t>::max()
 

Friends

class MultiCommand
 

Member Typedef Documentation

◆ CompleterClosure

using nix::Args::CompleterClosure = std::function<CompleterFun>
protected

The closure type of the completion callback.

This is what is actually stored as part of each Flag / Expected Arg.

◆ CompleterFun

using nix::Args::CompleterFun = void(AddCompletions &, size_t, std::string_view)
protected

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

Member Function Documentation

◆ description()

◆ doc()

◆ expectArg() [1/2]

void nix::Args::expectArg ( const std::string & label,
std::filesystem::path * dest,
bool optional = false )
inline

Expect a path argument.

◆ expectArg() [2/2]

void nix::Args::expectArg ( const std::string & label,
std::string * dest,
bool optional = false )
inline

Expect a string argument.

◆ expectArgs()

void nix::Args::expectArgs ( const std::string & label,
std::vector< std::string > * dest )
inline

Expect 0 or more arguments.

◆ getCommandBaseDir()

Path nix::Args::getCommandBaseDir ( ) const
virtual

Get the base directory for the command.

Returns
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.

Reimplemented in nix::RootArgs.

◆ getRoot()

RootArgs & nix::Args::getRoot ( )

Traverse parent pointers until we find the rootarguments" object.

◆ initialFlagsProcessed()

virtual void nix::Args::initialFlagsProcessed ( )
inlineprotectedvirtual

Called after all command line flags before the first non-flag argument (if any) have been processed.

◆ processArgs()

bool nix::Args::processArgs ( const Strings & args,
bool finish )
protectedvirtual

Process some positional arugments

Parameters
finishWe have parsed everything else, and these are the only arguments left. Used because we accumulate some "pending args" we might have left over.

Reimplemented in nix::LegacyArgs, and nix::MultiCommand.

◆ processFlag()

bool nix::Args::processFlag ( Strings::iterator & pos,
Strings::iterator end )
protectedvirtual

Process a single flag and its arguments, pulling from an iterator of raw CLI args as needed.

Reimplemented in nix::LegacyArgs, and nix::MultiCommand.

Member Data Documentation

◆ ArityAny

const size_t nix::Args::ArityAny = std::numeric_limits<size_t>::max()
staticprotected

The largest size_t is used to indicate the "any" arity, for handlers/flags/arguments that accept an arbitrary number of arguments.

◆ expectedArgs

std::list<ExpectedArg> nix::Args::expectedArgs
protected

Queue of expected positional argument forms.

Positional argument descriptions are inserted on the back.

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.

◆ longFlags

std::map<std::string, Flag::ptr> nix::Args::longFlags
protected

Index of all registered "long" flag descriptions (flags like --long).

◆ parent

MultiCommand* nix::Args::parent = nullptr

The parent command, used if this is a subcommand.

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.

◆ processedArgs

std::list<ExpectedArg> nix::Args::processedArgs
protected

List of processed positional argument forms.

All items removed from expectedArgs are added here. After all arguments were processed, this list should be exactly the same as expectedArgs was before.

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.

◆ shortFlags

std::map<char, Flag::ptr> nix::Args::shortFlags
protected

Index of all registered "short" flag descriptions (flags like -s).


The documentation for this class was generated from the following files: