cmd/cmd_parser: clean Int options handling
[nit.git] / src / doc / commands / commands_parser.nit
index 77fdd25..56e1081 100644 (file)
@@ -27,8 +27,11 @@ 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
@@ -137,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
@@ -157,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
@@ -178,56 +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(view)
-               if name == "main-compile" then return new CmdMainCompile(view)
-               if name == "main-run" then return new CmdManSynopsis(view)
-               if name == "main-opts" then return new CmdManOptions(view)
-               if name == "testing" then return new CmdTesting(view)
+               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
@@ -252,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
@@ -266,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
@@ -277,22 +283,26 @@ 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_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
@@ -300,7 +310,6 @@ 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
@@ -328,19 +337,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
@@ -363,6 +371,31 @@ 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
+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