浏览代码

[add] Manual page generation
[Closes #9] Doc / version / Man

isundil 9 年之前
父节点
当前提交
f7ab0dcc81
共有 7 个文件被更改,包括 226 次插入31 次删除
  1. 1 0
      .gitignore
  2. 81 9
      CMakeLists.txt
  3. 53 0
      doc/jsonstroll.1
  4. 14 0
      include/config.h
  5. 11 1
      include/params.hh
  6. 6 4
      src/main.cpp
  7. 60 17
      src/params.cpp

+ 1 - 0
.gitignore

@@ -41,4 +41,5 @@
 /Makefile
 .fuse_hidden*
 /vgcore.10462
+/install_manifest.txt
 

+ 81 - 9
CMakeLists.txt

@@ -1,25 +1,97 @@
 cmake_minimum_required(VERSION 2.8)
 
-add_executable(jsonstroll src/main.cpp src/jsonContainer.cpp src/warning.cpp src/params.cpp src/curseOutput.cpp src/linearHistory.cpp src/outputFlag.cpp src/streamConsumer.cpp src/jsonArray.cpp src/jsonObjectEntry.cpp src/jsonObject.cpp src/jsonElement.cpp src/jsonPrimitive.cpp src/jsonException.cpp)
-add_executable(json_test test/src/main.cpp src/jsonContainer.cpp src/warning.cpp src/linearHistory.cpp src/streamConsumer.cpp src/jsonArray.cpp src/jsonObjectEntry.cpp src/jsonObject.cpp src/jsonElement.cpp src/jsonPrimitive.cpp src/jsonException.cpp)
-add_executable(wrapped_test test/src/wrapped.cpp)
-
-set_property(TARGET jsonstroll PROPERTY RUNTIME_OUTPUT_DIRECTORY bin)
-
+SET (CUSTOM_BINARY_OUTPUT_DIR bin)
+SET (DOC_OUTPUT doc/)
 add_definitions ("-Wall")
 add_definitions ("-g3")
 
+add_executable(jsonstroll
+    src/main.cpp
+    src/jsonContainer.cpp
+    src/warning.cpp
+    src/params.cpp
+    src/curseOutput.cpp
+    src/linearHistory.cpp
+    src/outputFlag.cpp
+    src/streamConsumer.cpp
+    src/jsonArray.cpp
+    src/jsonObjectEntry.cpp
+    src/jsonObject.cpp
+    src/jsonElement.cpp
+    src/jsonPrimitive.cpp
+    src/jsonException.cpp
+    )
+
+set_property(TARGET jsonstroll PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CUSTOM_BINARY_OUTPUT_DIR})
+
 # Add ncurses
 SET(CURSES_NEED_NCURSES TRUE)
 find_package(Curses)
 target_link_libraries(jsonstroll ${ncurses++_LIBRARIES} ${CURSES_LIBRARIES})
 include_directories(include ${CURSES_INCLUDE_DIRS})
 
+# Add tests
 enable_testing()
-set_property(TARGET json_test PROPERTY RUNTIME_OUTPUT_DIRECTORY test)
+add_executable(json_test
+    test/src/main.cpp
+
+    src/jsonContainer.cpp
+    src/warning.cpp
+    src/linearHistory.cpp
+    src/streamConsumer.cpp
+    src/jsonArray.cpp
+    src/jsonObjectEntry.cpp
+    src/jsonObject.cpp
+    src/jsonElement.cpp
+    src/jsonPrimitive.cpp
+    src/jsonException.cpp
+    )
+set_property(
+    TARGET json_test
+    PROPERTY RUNTIME_OUTPUT_DIRECTORY test
+    )
+add_executable(wrapped_test
+    test/src/wrapped.cpp
+    )
 add_test(json_test test/json_test)
-set_property(TARGET wrapped_test PROPERTY RUNTIME_OUTPUT_DIRECTORY test)
+
+set_property(
+    TARGET wrapped_test
+    PROPERTY RUNTIME_OUTPUT_DIRECTORY test
+    )
 add_test(wrapped_test test/wrapped_test)
 
-install(TARGETS jsonstroll DESTINATION bin)
+# Add manual page
+find_program (HELP2MAN help2man)
+add_custom_target(man ALL)
+if (HELP2MAN)
+    add_definitions(
+        -DVERSIONDATE=__DATE__
+        )
+    add_custom_target(
+        TARGET man ALL
+        COMMAND ${CMAKE_COMMAND} -E make_directory ${DOC_OUTPUT}
+        )
+    add_custom_command(
+        TARGET man
+        SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${CUSTOM_BINARY_OUTPUT_DIR}/jsonstroll
+        COMMAND help2man ${CMAKE_CURRENT_BINARY_DIR}/${CUSTOM_BINARY_OUTPUT_DIR}/jsonstroll -o ${DOC_OUTPUT}/jsonstroll.1
+        OUTPUTS ${DOC_OUTPUT}/jsonstroll.1
+        )
+    add_custom_command(
+        TARGET man
+        SOURCE man
+        DEPENDS ${DOC_OUTPUT}/jsonstroll.1
+        )
+endif()
+
+# Install
+install(
+    TARGETS jsonstroll
+    DESTINATION bin
+    )
+install(
+    FILES ${DOC_OUTPUT}/jsonstroll.1
+    DESTINATION ${CMAKE_INSTALL_PREFIX}/man/man1
+    )
 

+ 53 - 0
doc/jsonstroll.1

@@ -0,0 +1,53 @@
+.\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.47.3.
+.TH JSONSTROLL "1" "July 2016" "jsonstroll (jsonstroller suite) 1.0RC1 generated on Jul 31 2016" "User Commands"
+.SH NAME
+jsonstroll \- manual page for jsonstroll (jsonstroller suite) 1.0RC1 generated on Jul 31 2016
+.SH SYNOPSIS
+.B jsonstroll
+[\fI\,OPTIONS\/\fR] [\fI\,--\/\fR] \fI\,INPUT\/\fR
+.SH DESCRIPTION
+Read json input and print it using ncurse
+.PP
+if not INPUT nor \fB\-f\fR, use standard input
+.TP
+\fB\-f\fR filename
+read input from filename instead of stdin
+.TP
+\fB\-W\fR
+consider continuing on non\-blocking errors
+.TP
+\fB\-\-ascii\fR
+ignore unicode values
+.TP
+\fB\-\-color\fR[=\fI\,MODE\/\fR]
+colorize output, MODE can be never or always (default when ommited)
+.TP
+\fB\-v\fR, \fB\-version\fR
+display version information
+.TP
+\fB\-h\fR, \fB\-\-helph\fR
+show this message and exit
+.SH EXAMPLES
+jsonstroll \-f f.json    Output f.json's content
+.SH AUTHOR
+Written by isundil <isundill@gmail.com>.
+.SH "REPORTING BUGS"
+Report bugs to <isundill@gmail.com>
+.SH COPYRIGHT
+Copyright \(co 2016 Free Software Foundation, Inc.
+License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
+.br
+This is free software: you are free to change and redistribute it.
+There is NO WARRANTY, to the extent permitted by law.
+.SH "SEE ALSO"
+The full documentation for
+.B jsonstroll
+is maintained as a Texinfo manual.  If the
+.B info
+and
+.B jsonstroll
+programs are properly installed at your site, the command
+.IP
+.B info jsonstroll
+.PP
+should give you access to the complete manual.

+ 14 - 0
include/config.h

@@ -18,3 +18,17 @@
 # define INDENT_LEVEL 4
 #endif //INDENT_LEVEL
 
+/**
+ * Version number
+**/
+#ifndef  VERSION
+# define VERSION "1.0RC1"
+#endif //VERSION
+#ifndef  VERSIONDATE
+# define VERSIONDATE "Jul 31 2016"
+#endif //VERSIONDATE
+
+#ifndef  STROLL_PROGNAME
+# define STROLL_PROGNAME "jsonstroll"
+#endif //STROLL_PROGNAME
+

+ 11 - 1
include/params.hh

@@ -33,6 +33,11 @@ class Params: public AParams
         Params(int ac, char **av);
         virtual ~Params();
 
+        /**
+         * Interpret input
+        **/
+        virtual bool read();
+
         /**
          * retun input
          * can be file stream (-f), stringstream ( -- INPUT), or std::cin (none)
@@ -47,7 +52,12 @@ class Params: public AParams
          * print usage
          * @param program name
         **/
-        static void usage(const std::string &) noexcept;
+        virtual void usage() const noexcept;
+        /**
+         * print version number
+         * @param program name
+        **/
+        virtual void version() const noexcept;
         /**
          * get argv[0]
         **/

+ 6 - 4
src/main.cpp

@@ -57,19 +57,21 @@ void run(Params *params)
 
 int main(int ac, char **av)
 {
-    Params *params;
+    Params *params = new Params(ac, av);
+    bool _run = false;
 
     try {
-        params = new Params(ac, av);
+        _run = params->read();
     }
     catch (std::runtime_error &e)
     {
         std::cerr << *av << ": " << e.what() << std::endl;
-        Params::usage(*av);
+        params->usage();
+        delete params;
         exit (EXIT_FAILURE);
     }
 
-    if (params->isValid())
+    if (_run)
         run(params);
     delete params;
     return 0;

+ 60 - 17
src/params.cpp

@@ -11,23 +11,36 @@
 #include <unistd.h>
 #include "params.hh"
 
-Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(ac -1)), strict(true)
+#include "config.h"
+
+Params::Params(int ac, char **av) :progName(*av), strict(true)
+{
+    av++;
+    while (*av)
+    {
+        params.push_back(*av);
+        av++;
+    }
+}
+
+bool Params::read()
 {
     bool written = false;
     std::stringstream *input = nullptr;
     ignoreUnicode = false;
-    colorMode = isatty(1);
+    colorMode = true;
 
-    while (*(++av))
+    for (std::list<std::string>::const_iterator i = params.cbegin(); i != params.cend(); i++)
     {
-        std::string tmp(*av);
+        std::string tmp(*i);
         if (!input)
         {
             if (tmp == "-f")
             {
-                tmp = *(++av);
-                if (!*av)
+                i++;
+                if (i == params.cend())
                     throw std::runtime_error("Invalid use of -f without argument");
+                tmp = *i;
                 std::ifstream *in = new std::ifstream(tmp);
                 if (!in->is_open())
                 {
@@ -46,15 +59,23 @@ Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(
                 ignoreUnicode = true;
             else if (tmp == "--color")
                 colorMode = true;
+            else if (tmp == "--help" || tmp == "-h")
+            {
+                usage();
+                return false;
+            }
+            else if (tmp == "--version" || tmp == "-v")
+            {
+                version();
+                return false;
+            }
             else if (tmp.find("--color=") == 0)
             {
-                std::string mode = (*av) + 8;
+                std::string mode = tmp.substr(8);
                 if (mode == "always")
                     colorMode = true;
                 else if (mode == "never")
                     colorMode = false;
-                else if (mode == "auto")
-                    colorMode = isatty(1);
                 else
                     throw std::runtime_error("Invalid option for --color: " +mode);
             }
@@ -72,6 +93,7 @@ Params::Params(int ac, char **av) :progName(*av), params(std::list<std::string>(
     }
     if (!this->input)
         this->input = input;
+    return true;
 }
 
 Params::~Params()
@@ -87,17 +109,38 @@ std::basic_istream<char> &Params::getInput() const
     return std::cin;
 }
 
-void Params::usage(const std::string &progName) noexcept
+void Params::usage() const noexcept
 {
-    std::cout << "Usage: " << progName << " [OPTIONS] [--] INPUT" << std::endl;
-    std::cout << "\t\t-f filename\tread input from (filename) instead of stdin" << std::endl;
-    std::cout << "\t\t--ascii\tignore unicode values" << std::endl;
-    std::cout << "\t\t--color[=MODE]\tColorize output, MODE can be always (default when ommited), never or auto (default if --color is ommited)" << std::endl;
-    std::cout << "\t\t-W\tdisable strict mode (warning does not interrupt reading)" << std::endl;
+    std::cout << "Usage: " << progName << " [OPTIONS] [--] INPUT" << std::endl
+    << "Read json input and print it using ncurse" << std::endl << std::endl
+    << "if not INPUT nor -f, use standard input" << std::endl << std::endl
+
+    << "  -f filename\t\tread input from filename instead of stdin" << std::endl
+    << "  -W \t\t\tconsider continuing on non-blocking errors" << std::endl
+    << "  --ascii\t\tignore unicode values" << std::endl
+    << "  --color[=MODE]\tcolorize output, MODE can be never or always (default when ommited)" << std::endl
+    << "  -v, -version\t\tdisplay version information" << std::endl
+    << "  -h, --helph\t\tshow this message and exit" << std::endl << std::endl
+
+    << "Examples:" << std::endl
+    << STROLL_PROGNAME << " -f f.json\tOutput f.json's content" << std::endl << std::endl
+
+    << "Report bugs to <isundill@gmail.com>" << std::endl;
 }
 
-bool Params::isValid() const
-{ return true; }
+void Params::version() const noexcept
+{
+    std::cout << STROLL_PROGNAME << " (jsonstroller suite) " << VERSION << " generated on " << VERSIONDATE << std::endl << std::endl
+
+    << "Copyright (C) 2016 Free Software Foundation, Inc." << std::endl
+    << "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>." << std::endl
+
+    << "This is free software: you are free to change and redistribute it." << std::endl
+    << "There is NO WARRANTY, to the extent permitted by law." << std::endl << std::endl
+
+    << "Written by isundil <isundill@gmail.com>." << std::endl;
+
+}
 
 bool Params::isStrict() const
 { return strict; }