Merge: nitrpg: Move `nitrpg` to its own repository
[nit.git] / lib / config / config.nit
index 463bf84..f07ec47 100644 (file)
@@ -188,9 +188,13 @@ class Config
        # Help option
        var opt_help = new OptionBool("Show this help message", "-h", "-?", "--help")
 
+       # Option --stub-man
+       var opt_stub_man = new OptionBool("Generate a stub manpage in markdown format", "--stub-man")
+
        # Redefine this init to add your options
        init do
-               add_option(opt_help)
+               add_option(opt_help, opt_stub_man)
+               opt_stub_man.hidden = true
        end
 
        # Add an option to `self`
@@ -201,6 +205,11 @@ class Config
        # Initialize `self` options from `args`
        fun parse_options(args: Collection[String]) do
                opts.parse(args)
+
+               if opt_stub_man.value then
+                       stub_man_options
+                       exit 0
+               end
        end
 
        # Return the remaining args once options are parsed by `from_args`
@@ -220,8 +229,42 @@ class Config
        # Display `tool_description` and options usage in console
        fun usage do
                print tool_description
+               print "\nOptions:"
                opts.usage
        end
+
+       # Generate a manpage stub from `self`
+       fun stub_man_options do
+               var lines = tool_description.split("\n")
+               var name = sys.program_name.basename
+               var syn = lines.shift
+               print "# NAME"
+               print ""
+               if lines.not_empty then
+                       printn "{name} - "
+                       print lines.join("\n")
+                       print ""
+               end
+               print "# SYNOPSIS"
+               print ""
+               print syn.replace("Usage: ", "")
+               print ""
+               print "# OPTIONS"
+               for o in opts.options do
+                       if o.hidden then continue
+
+                       var first = true
+                       print ""
+                       printn "### "
+                       for n in o.names do
+                               if first then first = false else printn ", "
+                               printn "`{n}`"
+                       end
+                       print ""
+                       print "{o.helptext}."
+               end
+               exit 0
+       end
 end
 
 # Configuration class based on a INI file.
@@ -256,7 +299,7 @@ class IniConfig
        super Config
 
        # Config tree used to store config options
-       var ini: ConfigTree is noinit
+       var ini: IniFile is noinit
 
        # Path to app config file
        var opt_config = new OptionString("Path to config file", "--config")
@@ -268,7 +311,7 @@ class IniConfig
 
        redef fun parse_options(args) do
                super
-               ini = new ConfigTree(config_file)
+               ini = new IniFile.from_file(config_file)
        end
 
        # Default config file path