cmd/cmd_parser: clean String options handling
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 15 May 2018 14:03:13 +0000 (10:03 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 21 Jun 2018 00:45:30 +0000 (20:45 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/commands/commands_parser.nit
src/doc/term/term.nit

index 97f1708..1db90f8 100644 (file)
@@ -140,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
@@ -255,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
@@ -280,22 +280,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
@@ -331,7 +335,8 @@ 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
@@ -366,6 +371,21 @@ 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
+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
index ea2954f..5010fc9 100644 (file)
@@ -26,7 +26,7 @@ redef class CommandParser
                # Translate links to doc commands
                if cmd isa CmdEntityLink then
                        cmd = new CmdComment(model, mentity_name = query)
-                       var opts = new HashMap[String, String]
+                       var opts = new CmdOptions
                        var status = cmd.parser_init(query, opts)
                        if not status isa CmdSuccess then error = status
                end