From 078e20885e0805e04669f9b334a516a6d8d12763 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 1 Dec 2003 15:55:05 +0000 Subject: [PATCH] * Help text for all (non-script) programs, so no more: $ nix-instantiate --help error: unknown flag `--help` Try `nix-instantiate --help' for more information. :-) --- scripts/nix-pull.in | 2 +- src/libmain/shared.cc | 39 +++++++++++++++++++++------------ src/libmain/shared.hh | 9 ++++++++ src/nix-env/Makefile.am | 5 +++++ src/nix-env/help.txt | 31 ++++++++++++++++++++++++++ src/nix-env/main.cc | 11 ++++++---- src/nix-hash/Makefile.am | 5 +++++ src/nix-hash/help.txt | 6 +++++ src/nix-hash/nix-hash.cc | 7 ++++++ src/nix-instantiate/Makefile.am | 5 +++++ src/nix-instantiate/help.txt | 15 +++++++++++++ src/nix-instantiate/main.cc | 11 +++++++--- src/nix-store/Makefile.am | 2 +- src/nix-store/main.cc | 10 +-------- 14 files changed, 126 insertions(+), 32 deletions(-) create mode 100644 src/nix-env/help.txt create mode 100644 src/nix-hash/help.txt create mode 100644 src/nix-instantiate/help.txt diff --git a/scripts/nix-pull.in b/scripts/nix-pull.in index ded4281cf..f1fb57e9b 100644 --- a/scripts/nix-pull.in +++ b/scripts/nix-pull.in @@ -99,7 +99,7 @@ sub processURL { if (scalar @ARGV > 0) { while (@ARGV) { my $url = shift @ARGV; - processURL $url; + processURL $url; } } else { open CONFFILE, "<$conffile"; diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 632794db3..b99a74bb4 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -23,31 +23,42 @@ static void initAndRun(int argc, char * * argv) nixDBPath = (string) NIX_STATE_DIR + "/db"; /* Put the arguments in a vector. */ - Strings args; + Strings args, remaining; while (argc--) args.push_back(*argv++); args.erase(args.begin()); /* Expand compound dash options (i.e., `-qlf' -> `-q -l -f'), and ignore options for the ATerm library. */ - for (Strings::iterator it = args.begin(); - it != args.end(); ) - { - string arg = *it; - if (string(arg, 0, 4) == "-at-") - it = args.erase(it); + for (Strings::iterator i = args.begin(); i != args.end(); ++i) { + string arg = *i; + if (string(arg, 0, 4) == "-at-") ; else if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') { - for (unsigned int i = 1; i < arg.length(); i++) - if (isalpha(arg[i])) - args.insert(it, (string) "-" + arg[i]); + for (unsigned int j = 1; j < arg.length(); j++) + if (isalpha(arg[j])) + remaining.push_back((string) "-" + arg[j]); else { - args.insert(it, string(arg, i)); + remaining.push_back(string(arg, j)); break; } - it = args.erase(it); - } else it++; + } else remaining.push_back(arg); + } + args = remaining; + remaining.clear(); + + /* Process default options. */ + for (Strings::iterator i = args.begin(); i != args.end(); ++i) { + string arg = *i; + if (arg == "--verbose" || arg == "-v") + verbosity = (Verbosity) ((int) verbosity + 1); + else if (arg == "--help") { + printHelp(); + return; + } else if (arg == "--keep-failed" || arg == "-K") + keepFailed = true; + else remaining.push_back(arg); } - run(args); + run(remaining); } diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh index 8ea637fd2..76b639e37 100644 --- a/src/libmain/shared.hh +++ b/src/libmain/shared.hh @@ -6,8 +6,17 @@ #include "util.hh" +/* These are not implemented here, but must be implemented by a + program linking against libmain. */ + +/* Main program. Called by main() after the ATerm library has been + initialised and some default arguments have been processed (and + removed from `args'). main() will catch all exceptions. */ void run(Strings args); +/* Should print a help message to stdout and return. */ +void printHelp(); + extern string programId; diff --git a/src/nix-env/Makefile.am b/src/nix-env/Makefile.am index add54581b..32e7a8111 100644 --- a/src/nix-env/Makefile.am +++ b/src/nix-env/Makefile.am @@ -6,6 +6,11 @@ nix_env_LDADD = ../libmain/libmain.a ../libexpr/libexpr.a \ ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx \ -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm +main.o: help.txt.hh + +%.txt.hh: %.txt + ../bin2c/bin2c helpText < $< > $@ || (rm $@ && exit 1) + AM_CXXFLAGS = \ -I.. -I../../externals/inst/include -I../libutil -I../libstore \ -I../libexpr -I../libmain diff --git a/src/nix-env/help.txt b/src/nix-env/help.txt new file mode 100644 index 000000000..69c474294 --- /dev/null +++ b/src/nix-env/help.txt @@ -0,0 +1,31 @@ +nix-env [OPTIONS...] [ARGUMENTS...] + +`nix-env' is a tool to manipulate Nix user environments. + +Operations: + + --install / -i FILE: add a derivation to the user environment + --uninstall / -u: remove a derivation to the user environment + --query / -q: perform a query on an environment or Nix expression + +The previous operations take a list of derivation names. The special +name `*' may be used to indicate all derivations. + + --version: output version information + --help: display help + +Query types: + + --name: print derivation names (default) + --expr / -e: print derivation store expression + --status / -s: print installed/present status + +Query sources: + + --installed: use installed derivations (default) + --available / -f FILE: use derivations available in expression FILE + +Options: + + --verbose / -v: verbose operation (may be repeated) + --keep-failed / -K: keep temporary directories of failed builds diff --git a/src/nix-env/main.cc b/src/nix-env/main.cc index 3c72a7950..829e2f4ae 100644 --- a/src/nix-env/main.cc +++ b/src/nix-env/main.cc @@ -3,6 +3,7 @@ #include "shared.hh" #include "parser.hh" #include "eval.hh" +#include "help.txt.hh" typedef void (* Operation) (EvalState & state, @@ -20,6 +21,12 @@ struct DrvInfo typedef map DrvInfos; +void printHelp() +{ + cout << string((char *) helpText, sizeof helpText); +} + + bool parseDerivation(EvalState & state, Expr e, DrvInfo & drv) { ATMatcher m; @@ -395,10 +402,6 @@ void run(Strings args) op = opUninstall; else if (arg == "--query" || arg == "-q") op = opQuery; - else if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg[0] == '-') - opFlags.push_back(arg); else opArgs.push_back(arg); diff --git a/src/nix-hash/Makefile.am b/src/nix-hash/Makefile.am index 8609cb216..154f7a276 100644 --- a/src/nix-hash/Makefile.am +++ b/src/nix-hash/Makefile.am @@ -4,5 +4,10 @@ nix_hash_SOURCES = nix-hash.cc nix_hash_LDADD = ../libmain/libmain.a ../libstore/libstore.a ../libutil/libutil.a \ ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx -lATerm +nix-hash.o: help.txt.hh + +%.txt.hh: %.txt + ../bin2c/bin2c helpText < $< > $@ || (rm $@ && exit 1) + AM_CXXFLAGS = \ -I.. -I../../externals/inst/include -I../libutil -I../libstore -I../libmain diff --git a/src/nix-hash/help.txt b/src/nix-hash/help.txt new file mode 100644 index 000000000..84ba152c5 --- /dev/null +++ b/src/nix-hash/help.txt @@ -0,0 +1,6 @@ +nix-hash [OPTIONS...] [FILES...] + +`nix-hash computes and prints cryptographic hashes for the specified +files. + + --flat: compute hash of regular file contents, not metadata diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc index 77c169b9a..23309ff74 100644 --- a/src/nix-hash/nix-hash.cc +++ b/src/nix-hash/nix-hash.cc @@ -2,6 +2,13 @@ #include "hash.hh" #include "shared.hh" +#include "help.txt.hh" + + +void printHelp() +{ + cout << string((char *) helpText, sizeof helpText); +} void run(Strings args) diff --git a/src/nix-instantiate/Makefile.am b/src/nix-instantiate/Makefile.am index 91843f663..7a04e932e 100644 --- a/src/nix-instantiate/Makefile.am +++ b/src/nix-instantiate/Makefile.am @@ -6,6 +6,11 @@ nix_instantiate_LDADD = ../libmain/libmain.a ../libexpr/libexpr.a \ ../boost/format/libformat.a -L../../externals/inst/lib -ldb_cxx \ -lsglr -lATB -lconversion -lasfix2 -lmept -lATerm +main.o: help.txt.hh + +%.txt.hh: %.txt + ../bin2c/bin2c helpText < $< > $@ || (rm $@ && exit 1) + AM_CXXFLAGS = \ -I.. -I../../externals/inst/include -I../libutil -I../libstore \ -I../libexpr -I../libmain diff --git a/src/nix-instantiate/help.txt b/src/nix-instantiate/help.txt new file mode 100644 index 000000000..fbe9d92db --- /dev/null +++ b/src/nix-instantiate/help.txt @@ -0,0 +1,15 @@ +nix-instantiate [OPTIONS...] [FILES...] + +`nix-instantiate' turns Nix expressions into store expressions. + +The argument `-' may be specified to read a Nix expression from +standard input. + +Options: + + --version: output version information + --help: display help + +Options: + + --verbose / -v: verbose operation (may be repeated) diff --git a/src/nix-instantiate/main.cc b/src/nix-instantiate/main.cc index 50a4991a5..305c3b551 100644 --- a/src/nix-instantiate/main.cc +++ b/src/nix-instantiate/main.cc @@ -6,6 +6,13 @@ #include "shared.hh" #include "eval.hh" #include "parser.hh" +#include "help.txt.hh" + + +void printHelp() +{ + cout << string((char *) helpText, sizeof helpText); +} #if 0 @@ -87,9 +94,7 @@ void run(Strings args) } else #endif - if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg == "-") + if (arg == "-") readStdin = true; else if (arg[0] == '-') throw UsageError(format("unknown flag `%1%`") % arg); diff --git a/src/nix-store/Makefile.am b/src/nix-store/Makefile.am index 3738c53ca..80e598742 100644 --- a/src/nix-store/Makefile.am +++ b/src/nix-store/Makefile.am @@ -6,7 +6,7 @@ nix_store_LDADD = ../libmain/libmain.a ../libstore/libstore.a ../libutil/libutil main.o: help.txt.hh -%.hh: % +%.txt.hh: %.txt ../bin2c/bin2c helpText < $< > $@ || (rm $@ && exit 1) AM_CXXFLAGS = \ diff --git a/src/nix-store/main.cc b/src/nix-store/main.cc index c73de5289..48752c2bf 100644 --- a/src/nix-store/main.cc +++ b/src/nix-store/main.cc @@ -12,14 +12,12 @@ typedef void (* Operation) (Strings opFlags, Strings opArgs); -static void printHelp() +void printHelp() { cout << string((char *) helpText, sizeof helpText); - exit(0); } - static Path checkPath(const Path & arg) { return arg; /* !!! check that arg is in the store */ @@ -276,12 +274,6 @@ void run(Strings args) op = opInit; else if (arg == "--verify") op = opVerify; - else if (arg == "--verbose" || arg == "-v") - verbosity = (Verbosity) ((int) verbosity + 1); - else if (arg == "--keep-failed" || arg == "-K") - keepFailed = true; - else if (arg == "--help") - printHelp(); else if (arg[0] == '-') opFlags.push_back(arg); else