cmd/cmd_parser: clean Bool options handling
[nit.git] / src / doc / commands / commands_parser.nit
index 7ae94e2..6678bfa 100644 (file)
@@ -22,12 +22,16 @@ import commands::commands_graph
 import commands::commands_usage
 import commands::commands_catalog
 import commands::commands_ini
+import commands::commands_main
 
 # Parse string commands to create DocQueries
 class CommandParser
 
-       # ModelView used to retrieve mentities
-       var view: ModelView
+       # Model used to retrieve mentities
+       var model: Model
+
+       # Main module for linearization
+       var mainmodule: MModule
 
        # ModelBuilder used to retrieve AST nodes
        var modelbuilder: ModelBuilder
@@ -42,6 +46,7 @@ class CommandParser
        "param", "return", "new", "call", "defs", "list", "random",
        "ini-desc", "ini-git", "ini-issues", "ini-maintainer", "ini-contributors", "ini-license",
        "license-file", "contrib-file", "license-content", "contrib-content", "git-clone",
+       "mains", "main-compile", "main-run", "main-opts", "testing",
        "catalog", "stats", "tags", "tag", "person", "contrib", "maintain"] is writable
 
        # List of commands usage and documentation
@@ -83,6 +88,12 @@ class CommandParser
                usage["contrib-file: <package>"] = "display the contrib file for the `package`"
                usage["contrib-content: <package>"] = "display the contrib file content for the `package`"
                usage["git-clone: <package>"] = "display the git clone command for the `package`"
+               # Main
+               usage["mains: <name>"] = "display the list of main methods for `name`"
+               usage["main-compile: <name>"] = "display the nitc command to compile `name`"
+               usage["main-run: <name>"] = "display the command to run `name`"
+               usage["main-opts: <name>"] = "display the command options for `name`"
+               usage["testing: <name>"] = "display the nitunit command to test `name`"
                return usage
        end
 
@@ -129,7 +140,7 @@ class CommandParser
                end
 
                # Parse command options
-               var opts = new HashMap[String, String]
+               var opts = new CmdOptions
                while pos < string.length do
                        # Parse option name
                        tmp.clear
@@ -149,7 +160,7 @@ class CommandParser
                # Build the command
                var command
                if is_short_link then
-                       command = new CmdEntityLink(view)
+                       command = new CmdEntityLink(model)
                else
                        command = new_command(name)
                end
@@ -170,50 +181,56 @@ class CommandParser
        # You must redefine this method to add new custom commands.
        fun new_command(name: String): nullable DocCommand do
                # CmdEntity
-               if name == "link" then return new CmdEntityLink(view)
-               if name == "doc" then return new CmdComment(view)
-               if name == "code" then return new CmdEntityCode(view, modelbuilder)
-               if name == "lin" then return new CmdLinearization(view)
-               if name == "defs" then return new CmdFeatures(view)
-               if name == "parents" then return new CmdParents(view)
-               if name == "ancestors" then return new CmdAncestors(view)
-               if name == "children" then return new CmdChildren(view)
-               if name == "descendants" then return new CmdDescendants(view)
-               if name == "param" then return new CmdParam(view)
-               if name == "return" then return new CmdReturn(view)
-               if name == "new" then return new CmdNew(view, modelbuilder)
-               if name == "call" then return new CmdCall(view, modelbuilder)
+               if name == "link" then return new CmdEntityLink(model)
+               if name == "doc" then return new CmdComment(model)
+               if name == "code" then return new CmdEntityCode(model, modelbuilder)
+               if name == "lin" then return new CmdLinearization(model, mainmodule)
+               if name == "defs" then return new CmdFeatures(model)
+               if name == "parents" then return new CmdParents(model, mainmodule)
+               if name == "ancestors" then return new CmdAncestors(model, mainmodule)
+               if name == "children" then return new CmdChildren(model, mainmodule)
+               if name == "descendants" then return new CmdDescendants(model, mainmodule)
+               if name == "param" then return new CmdParam(model)
+               if name == "return" then return new CmdReturn(model)
+               if name == "new" then return new CmdNew(model, modelbuilder)
+               if name == "call" then return new CmdCall(model, modelbuilder)
                # CmdGraph
-               if name == "uml" then return new CmdUML(view)
-               if name == "graph" then return new CmdInheritanceGraph(view)
+               if name == "uml" then return new CmdUML(model, mainmodule)
+               if name == "graph" then return new CmdInheritanceGraph(model, mainmodule)
                # CmdModel
-               if name == "list" then return new CmdModelEntities(view)
-               if name == "random" then return new CmdRandomEntities(view)
+               if name == "list" then return new CmdModelEntities(model)
+               if name == "random" then return new CmdRandomEntities(model)
                # Ini
-               if name == "ini-desc" then return new CmdIniDescription(view)
-               if name == "ini-git" then return new CmdIniGitUrl(view)
-               if name == "ini-issues" then return new CmdIniIssuesUrl(view)
-               if name == "ini-license" then return new CmdIniLicense(view)
-               if name == "ini-maintainer" then return new CmdIniMaintainer(view)
-               if name == "ini-contributors" then return new CmdIniContributors(view)
-               if name == "license-file" then return new CmdLicenseFile(view)
-               if name == "license-content" then return new CmdLicenseFileContent(view)
-               if name == "contrib-file" then return new CmdContribFile(view)
-               if name == "contrib-content" then return new CmdContribFileContent(view)
-               if name == "git-clone" then return new CmdIniCloneCommand(view)
+               if name == "ini-desc" then return new CmdIniDescription(model)
+               if name == "ini-git" then return new CmdIniGitUrl(model)
+               if name == "ini-issues" then return new CmdIniIssuesUrl(model)
+               if name == "ini-license" then return new CmdIniLicense(model)
+               if name == "ini-maintainer" then return new CmdIniMaintainer(model)
+               if name == "ini-contributors" then return new CmdIniContributors(model)
+               if name == "license-file" then return new CmdLicenseFile(model)
+               if name == "license-content" then return new CmdLicenseFileContent(model)
+               if name == "contrib-file" then return new CmdContribFile(model)
+               if name == "contrib-content" then return new CmdContribFileContent(model)
+               if name == "git-clone" then return new CmdIniCloneCommand(model)
+               # CmdMain
+               if name == "mains" then return new CmdMains(model)
+               if name == "main-compile" then return new CmdMainCompile(model)
+               if name == "main-run" then return new CmdManSynopsis(model)
+               if name == "main-opts" then return new CmdManOptions(model)
+               if name == "testing" then return new CmdTesting(model)
                # CmdCatalog
                var catalog = self.catalog
                if catalog != null then
-                       if name == "catalog" then return new CmdCatalogPackages(view, catalog)
-                       if name == "stats" then return new CmdCatalogStats(view, catalog)
-                       if name == "tags" then return new CmdCatalogTags(view, catalog)
-                       if name == "tag" then return new CmdCatalogTag(view, catalog)
-                       if name == "person" then return new CmdCatalogPerson(view, catalog)
-                       if name == "contrib" then return new CmdCatalogContributing(view, catalog)
-                       if name == "maintain" then return new CmdCatalogMaintaining(view, catalog)
-                       if name == "search" then return new CmdCatalogSearch(view, catalog)
+                       if name == "catalog" then return new CmdCatalogPackages(model, catalog)
+                       if name == "stats" then return new CmdCatalogStats(model, catalog)
+                       if name == "tags" then return new CmdCatalogTags(model, catalog)
+                       if name == "tag" then return new CmdCatalogTag(model, catalog)
+                       if name == "person" then return new CmdCatalogPerson(model, catalog)
+                       if name == "contrib" then return new CmdCatalogContributing(model, catalog)
+                       if name == "maintain" then return new CmdCatalogMaintaining(model, catalog)
+                       if name == "search" then return new CmdCatalogSearch(model, catalog)
                else
-                       if name == "search" then return new CmdSearch(view)
+                       if name == "search" then return new CmdSearch(model)
                end
                return null
        end
@@ -238,7 +255,7 @@ end
 redef class DocCommand
 
        # Initialize the command from the CommandParser data
-       fun parser_init(arg: String, options: Map[String, String]): CmdMessage do
+       fun parser_init(arg: String, options: CmdOptions): CmdMessage do
                return init_command
        end
 end
@@ -252,7 +269,10 @@ end
 
 redef class CmdList
        redef fun parser_init(mentity_name, options) do
-               if options.has_key("limit") and options["limit"].is_int then limit = options["limit"].to_i
+               var opt_page = options.opt_int("page")
+               if opt_page != null then page = opt_page
+               var opt_limit = options.opt_int("limit")
+               if opt_limit != null then limit = opt_limit
                return super
        end
 end
@@ -261,24 +281,30 @@ end
 
 redef class CmdComment
        redef fun parser_init(mentity_name, options) do
-               full_doc = not options.has_key("only-synopsis")
-               fallback = not options.has_key("no-fallback")
-               if options.has_key("format") then format = options["format"]
+               var opt_full_doc = options.opt_bool("only-synopsis")
+               if opt_full_doc != null then full_doc = not opt_full_doc
+               var opt_fallback = options.opt_bool("no-fallback")
+               if opt_fallback != null then fallback = not opt_fallback
+               var opt_format = options.opt_string("format")
+               if opt_format != null then format = opt_format
                return super
        end
 end
 
 redef class CmdEntityLink
        redef fun parser_init(mentity_name, options) do
-               if options.has_key("text") then text = options["text"]
-               if options.has_key("title") then title = options["title"]
+               var opt_text = options.opt_string("text")
+               if opt_text != null then text = opt_text
+               var opt_title = options.opt_string("title")
+               if opt_title != null then title = opt_title
                return super
        end
 end
 
 redef class CmdCode
        redef fun parser_init(mentity_name, options) do
-               if options.has_key("format") then format = options["format"]
+               var opt_format = options.opt_string("format")
+               if opt_format != null then format = opt_format
                return super
        end
 end
@@ -286,21 +312,22 @@ end
 redef class CmdSearch
        redef fun parser_init(mentity_name, options) do
                query = mentity_name
-               if options.has_key("page") and options["page"].is_int then page = options["page"].to_i
                return super
        end
 end
 
 redef class CmdAncestors
        redef fun parser_init(mentity_name, options) do
-               if options.has_key("parents") and options["parents"] == "false" then parents = false
+               var opt_parents = options.opt_bool("no-parents")
+               if opt_parents != null then parents = not opt_parents
                return super
        end
 end
 
 redef class CmdDescendants
        redef fun parser_init(mentity_name, options) do
-               if options.has_key("children") and options["children"] == "false" then children = false
+               var opt_children = options.opt_bool("no-children")
+               if opt_children != null then children = not opt_children
                return super
        end
 end
@@ -314,19 +341,18 @@ end
 
 redef class CmdGraph
        redef fun parser_init(mentity_name, options) do
-               if options.has_key("format") then format = options["format"]
+               var opt_format = options.opt_string("format")
+               if opt_format != null then format = opt_format
                return super
        end
 end
 
 redef class CmdInheritanceGraph
        redef fun parser_init(mentity_name, options) do
-               if options.has_key("pdepth") and options["pdepth"].is_int then
-                       pdepth = options["pdepth"].to_i
-               end
-               if options.has_key("cdepth") and options["cdepth"].is_int then
-                       cdepth = options["cdepth"].to_i
-               end
+               var opt_pdepth = options.opt_int("pdepth")
+               if opt_pdepth != null then pdepth = opt_pdepth
+               var opt_cdepth = options.opt_int("cdepth")
+               if opt_cdepth != null then cdepth = opt_cdepth
                return super
        end
 end
@@ -349,6 +375,44 @@ end
 
 # Utils
 
+# Commands options
+class CmdOptions
+       super HashMap[String,  String]
+
+       # Get option value for `key` as String
+       #
+       # Return `null` if no option with that `key` or if value is empty.
+       fun opt_string(key: String): nullable String do
+               if not has_key(key) then return null
+               var value = self[key]
+               if value.is_empty then return null
+               return value
+       end
+
+       # Get option value for `key` as Int
+       #
+       # Return `null` if no option with that `key` or if value is not an Int.
+       fun opt_int(key: String): nullable Int do
+               if not has_key(key) then return null
+               var value = self[key]
+               if not value.is_int then return null
+               return value.to_i
+       end
+
+       # Get option value as bool
+       #
+       # Return `true` if the value with that `key` is empty or equals `"true"`.
+       # Return `false` if the value with that `key` equals `"false"`.
+       # Return `null` in any other case.
+       fun opt_bool(key: String): nullable Bool do
+               if not has_key(key) then return null
+               var value = self[key]
+               if value.is_empty or value == "true" then return true
+               if value == "false" then return false
+               return null
+       end
+end
+
 redef class Text
        # Read `self` as raw text until `nend` and append it to the `out` buffer.
        private fun read_until(out: FlatBuffer, start: Int, nend: Char...): Int do