cmd/cmd_parser: clean Bool options handling
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 15 May 2018 15:46:04 +0000 (11:46 -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/commands/tests/test_commands_parser.nit
src/doc/term/tests/test_term.nit

index 56e1081..6678bfa 100644 (file)
@@ -281,8 +281,10 @@ 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")
+               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
@@ -316,14 +318,16 @@ 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
@@ -394,6 +398,19 @@ class CmdOptions
                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
index 42f715d..d97e281 100644 (file)
@@ -160,7 +160,7 @@ class TestCommandsParser
 
        fun test_cmd_parser_ancestors_without_parents is test do
                var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
-               var cmd = parser.parse("ancestors: test_prog::Warrior | parents: false")
+               var cmd = parser.parse("ancestors: test_prog::Warrior | no-parents")
                assert cmd isa CmdAncestors
                assert parser.error == null
                assert cmd.results.as(not null).length == 1
@@ -184,7 +184,7 @@ class TestCommandsParser
 
        fun test_cmd_parser_descendants_without_children is test do
                var parser = new CommandParser(test_model, test_main, test_builder, test_catalog)
-               var cmd = parser.parse("descendants: Object | children: false")
+               var cmd = parser.parse("descendants: Object | no-children: true")
                assert cmd isa CmdDescendants
                assert parser.error == null
                assert cmd.results.as(not null).length == 9
index 4d7481e..1b507fe 100644 (file)
@@ -71,12 +71,12 @@ class TestTerm
 
        fun test_ancestors is test do
                var parser = new CommandParser(test_model, test_main, test_builder)
-               parser.execute("ancestors: Warrior | parents: true", true)
+               parser.execute("ancestors: Warrior | no-parents: false", true)
        end
 
        fun test_ancestors_without_parents is test do
                var parser = new CommandParser(test_model, test_main, test_builder)
-               parser.execute("ancestors: Warrior | parents: false", true)
+               parser.execute("ancestors: Warrior | no-parents", true)
        end
 
        fun test_parents is test do
@@ -96,7 +96,7 @@ class TestTerm
 
        fun test_descendants_without_children is test do
                var parser = new CommandParser(test_model, test_main, test_builder)
-               parser.execute("descendants: Career | children: false", true)
+               parser.execute("descendants: Career | no-children", true)
        end
 
        # CmdLin