From cc62c09409021582d8b5ffe3b5c9d393524fd260 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 11 Oct 2009 15:45:06 +0200 Subject: Add usage information --- cli/cli.cxx | 29 +++++++++ cli/generator.cxx | 58 +++++++++++++++++ cli/generator.hxx | 3 + cli/options.cli | 3 +- cli/options.cxx | 12 ++-- cli/options.hxx | 8 +-- cli/options.ixx | 12 ++-- cli/usage.hxx | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 289 insertions(+), 17 deletions(-) create mode 100644 cli/usage.hxx diff --git a/cli/cli.cxx b/cli/cli.cxx index 4aaa2d6..b2c6695 100644 --- a/cli/cli.cxx +++ b/cli/cli.cxx @@ -7,11 +7,15 @@ #include #include +#include + +#include "usage.hxx" #include "options.hxx" #include "parser.hxx" #include "generator.hxx" using namespace std; +using namespace cutl; int main (int argc, char* argv[]) { @@ -22,6 +26,8 @@ int main (int argc, char* argv[]) int end; options ops (argc, argv, end); + // Handle --version + // if (ops.version ()) { e << "CodeSynthesis CLI command line interface compiler 0.0.2" << endl @@ -34,6 +40,29 @@ int main (int argc, char* argv[]) return 0; } + // Handle --help + // + if (ops.help ()) + { + e << "Usage: " << argv[0] << " [options] file" + << endl + << "Options:" << endl; + + compiler::ostream_filter filt (e); + + e << "--help" << endl + << " Print usage information and exit." + << endl; + + e << "--version" << endl + << " Print version and exit." + << endl; + + generator::usage (); + + return 0; + } + if (end == argc) { e << "error: no input file specified" << endl diff --git a/cli/generator.cxx b/cli/generator.cxx index 801f7e5..89270bb 100644 --- a/cli/generator.cxx +++ b/cli/generator.cxx @@ -31,6 +31,64 @@ using namespace cutl; using semantics::path; +void generator:: +usage () +{ + ostream& e (cerr); + + e << "--output-dir | -o " << endl + << " Write generated files to ." << endl; + + e << "--suppress-inline" << endl + << " Generate all functions non-inline." << endl; + + e << "--hxx-suffix " << endl + << " Use instead of the default '.hxx' to\n" + << " construct the name of the generated header file." + << endl; + + e << "--ixx-suffix " << endl + << " Use instead of the default '.ixx' to\n" + << " construct the name of the generated inline file." + << endl; + + e << "--cxx-suffix " << endl + << " Use instead of the default '.cxx' to\n" + << " construct the name of the generated source file." + << endl; + + e << "--option-prefix " << endl + << " Use instead of the default '-' as an\n" + << " option prefix." + << endl; + + e << "--option-separator " << endl + << " Use instead of the default '--' as an\n" + << " optional separator between options and arguments." + << endl; + + e << "--include-with-brackets" << endl + << " Use angle brackets (<>) instead of quotes (\"\") in\n" + << " generated #include directives." + << endl; + + e << "--include-prefix " << endl + << " Add to generated #include directive\n" + << " paths." + << endl; + + e << "--guard-prefix " << endl + << " Add to generated header inclusion guards." + << endl; + + e << "--reserved-name " << endl + << " Add to the list of names that should not\n" + << " be used as identifiers. The name can optionally\n" + << " be followed by '=' and the replacement name that\n" + << " should be used instead." + << endl; +} + namespace { static char const header[] = diff --git a/cli/generator.hxx b/cli/generator.hxx index c9b449d..112c20d 100644 --- a/cli/generator.hxx +++ b/cli/generator.hxx @@ -14,6 +14,9 @@ class generator public: generator (); + static void + usage (); + class failed {}; void diff --git a/cli/options.cli b/cli/options.cli index e2549fa..a5fe0ae 100644 --- a/cli/options.cli +++ b/cli/options.cli @@ -16,9 +16,10 @@ class options bool --help; bool --version; + std::string --output-dir | -o; + bool --suppress-inline; - std::string --output-dir | -o; std::string --hxx-suffix = ".hxx"; std::string --ixx-suffix = ".ixx"; std::string --cxx-suffix = ".cxx"; diff --git a/cli/options.cxx b/cli/options.cxx index d5e7e67..4264369 100644 --- a/cli/options.cxx +++ b/cli/options.cxx @@ -240,8 +240,8 @@ options (int argc, ::cli::unknown_mode arg) : help_ (), version_ (), - suppress_inline_ (), output_dir_ (), + suppress_inline_ (), hxx_suffix_ (".hxx"), ixx_suffix_ (".ixx"), cxx_suffix_ (".cxx"), @@ -263,8 +263,8 @@ options (int start, ::cli::unknown_mode arg) : help_ (), version_ (), - suppress_inline_ (), output_dir_ (), + suppress_inline_ (), hxx_suffix_ (".hxx"), ixx_suffix_ (".ixx"), cxx_suffix_ (".cxx"), @@ -286,8 +286,8 @@ options (int argc, ::cli::unknown_mode arg) : help_ (), version_ (), - suppress_inline_ (), output_dir_ (), + suppress_inline_ (), hxx_suffix_ (".hxx"), ixx_suffix_ (".ixx"), cxx_suffix_ (".cxx"), @@ -310,8 +310,8 @@ options (int start, ::cli::unknown_mode arg) : help_ (), version_ (), - suppress_inline_ (), output_dir_ (), + suppress_inline_ (), hxx_suffix_ (".hxx"), ixx_suffix_ (".ixx"), cxx_suffix_ (".cxx"), @@ -339,12 +339,12 @@ struct _cli_options_map_init &::cli::thunk; _cli_options_map_["--version"] = &::cli::thunk; - _cli_options_map_["--suppress-inline"] = - &::cli::thunk; _cli_options_map_["--output-dir"] = &::cli::thunk; _cli_options_map_["-o"] = &::cli::thunk; + _cli_options_map_["--suppress-inline"] = + &::cli::thunk; _cli_options_map_["--hxx-suffix"] = &::cli::thunk; _cli_options_map_["--ixx-suffix"] = diff --git a/cli/options.hxx b/cli/options.hxx index b856fbc..4d33939 100644 --- a/cli/options.hxx +++ b/cli/options.hxx @@ -179,12 +179,12 @@ class options bool const& version () const; - bool const& - suppress_inline () const; - std::string const& output_dir () const; + bool const& + suppress_inline () const; + std::string const& hxx_suffix () const; @@ -223,8 +223,8 @@ class options public: bool help_; bool version_; - bool suppress_inline_; std::string output_dir_; + bool suppress_inline_; std::string hxx_suffix_; std::string ixx_suffix_; std::string cxx_suffix_; diff --git a/cli/options.ixx b/cli/options.ixx index 0ca4c9e..7ec5579 100644 --- a/cli/options.ixx +++ b/cli/options.ixx @@ -100,18 +100,18 @@ version () const return version_; } -inline bool const& options:: -suppress_inline () const -{ - return suppress_inline_; -} - inline std::string const& options:: output_dir () const { return output_dir_; } +inline bool const& options:: +suppress_inline () const +{ + return suppress_inline_; +} + inline std::string const& options:: hxx_suffix () const { diff --git a/cli/usage.hxx b/cli/usage.hxx new file mode 100644 index 0000000..3aeb30b --- /dev/null +++ b/cli/usage.hxx @@ -0,0 +1,181 @@ +// file : cli/usage.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CLI_USAGE_HXX +#define CLI_USAGE_HXX + +#include // std::size_t +#include // std::abort + +#include + +template +class usage_indenter: public cutl::compiler::code_stream +{ +public: + usage_indenter (cutl::compiler::code_stream& out) + : out_ (out), + option_length_ (0), + construct_ (con_newline) + { + } + +private: + usage_indenter (usage_indenter const&); + + usage_indenter& + operator= (usage_indenter const&); + +public: + virtual void + put (C c) + { + switch (c) + { + case '\n': + { + switch (construct_) + { + case con_newline: + { + out_.put (c); + break; + } + case con_option: + { + construct_ = con_newline; + break; + } + case con_description: + { + out_.put (c); + construct_ = con_newline; + break; + } + default: + { + std::abort (); + } + } + + break; + } + case '-': + { + switch (construct_) + { + case con_newline: + { + construct_ = con_option; + option_length_ = 0; + output_indentation (); + out_.put (c); + ++option_length_; + break; + } + case con_option: + { + ++option_length_; + //fall through + } + case con_description: + { + out_.put (c); + break; + } + default: + { + std::abort (); + } + } + + break; + } + default: + { + switch (construct_) + { + case con_newline: + { + construct_ = con_description; + output_indentation (); + out_.put (c); + break; + } + case con_option: + { + ++option_length_; + //fall through + } + default: + { + out_.put (c); + break; + } + } + + break; + } + } + } + + virtual void + unbuffer () + { + } + +private: + void + output_indentation () + { + std::size_t spaces; + + switch (construct_) + { + case con_option: + { + spaces = 2; + option_length_ += 2; + break; + } + case con_description: + { + spaces = 29; + + if (option_length_) + { + if (option_length_ > spaces) + spaces = 1; + else + spaces -= option_length_; + + option_length_ = 0; + } + + break; + } + default: + { + std::abort (); + } + } + + while (spaces--) + out_.put (' '); + } + +private: + cutl::compiler::code_stream& out_; + size_t option_length_; + + enum construct + { + con_newline, + con_option, + con_description + } construct_; +}; + +#endif // CLI_USAGE_HXX -- cgit v1.1