Repository: comonicon/Comonicon.jl (julia)

Issue:
in correct markdown flag and argument parsing 
When I was doing my MIS emulation CLI on server, the new markdown doc parsing seems not recognizing `-o,--option=<hint>` pattern correctly

Pull request description:
fix #144

Patch:
diff --git a/src/markdown.jl b/src/markdown.jl
index b441e6cc..4303290e 100644
--- a/src/markdown.jl
+++ b/src/markdown.jl
@@ -56,8 +56,8 @@ function read_options(md::Markdown.MD)
     sec === nothing && return options
 
     for each in sec.items
-        name, doc = read_item(each[1])
-        name, short, hint = split_option(name)
+        code, doc = read_item(each[1])
+        name, short, hint = split_option(code)
         options[name] = JLMDOption(hint, doc, !isnothing(short))
     end
     return options
@@ -157,25 +157,26 @@ function split_option(content::String)
     names = map(strip, split(content, ",")) # rm space
 
     if length(names) == 1 # long option
-        name, hint = split_hint(lstrip(names[1], '-'))
+        long, hint = split_hint(lstrip(names[1], '-'))
         short = nothing
     elseif length(names) == 2 # short option
-        idx = findfirst(startswith("--"), names)
-        idx === nothing && error("cannot find long option, got $content")
-        name = lstrip(names[idx], '-')
-        short = idx == 1 ? names[2] : names[1]
-        short = lstrip(short, '-')
+        if startswith(names[1], "--") && !startswith(names[2], "--") && startswith(names[2], '-') # --option, -o
+            long, (short, hint) = lstrip(names[1], '-'), split_hint(lstrip(names[2], '-'))
+        elseif startswith(names[2], "--") && !startswith(names[1], "--") && startswith(names[1], '-') # -o, --option
+            (long, hint), short = split_hint(lstrip(names[2], '-')), lstrip(names[1], '-')
+        else
+            error("expect --option[, -o], got $content")
+        end
 
-        short, hint = split_hint(lstrip(short, '-'))
         length(short) == 1 ||
-            throw(Meta.ParseError("short option can only use one letter, got --$content"))
-        first(short) == first(name) ||
-            throw(Meta.ParseError("short option must use the same first letter, got --$content"))
+            throw(Meta.ParseError("short option can only use one letter, got $content"))
+        first(short) == first(long) ||
+            throw(Meta.ParseError("short option must use the same first letter, got $content"))
     else
-        throw(Meta.ParseError("too much inputs, expect --option[,-o], got --$content"))
+        throw(Meta.ParseError("too much inputs, expect --option[,-o], got $content"))
     end
-    name = replace(name, '_' => '-')
-    return name, short, hint
+    long = replace(long, '_' => '-')
+    return long, short, hint
 end
 
 function split_hint(content::AbstractString)


Test patch:
diff --git a/test/markdown.jl b/test/markdown.jl
index 96dbb4b3..a9db8a8c 100644
--- a/test/markdown.jl
+++ b/test/markdown.jl
@@ -169,3 +169,29 @@ end
     @test doc.flags["long"] == JLMDFlag("long flag.", false)
     @test doc.options["long"] == JLMDOption(nothing, "long option using default hint.", false)
 end
+
+@testset "reverse order" begin
+    content = Markdown.parse("""
+    description of the command.
+    
+    # Options
+    
+    - `-o, --option=<value>`: some random option.
+    - `-o,--option_space=<value>`: some random option.
+    """)
+    
+    doc = split_docstring(content)
+    
+    @test doc.options["option"] == JLMDOption("value", "some random option.", true)
+    @test doc.options["option-space"] == JLMDOption("value", "some random option.", true)
+
+    content = Markdown.parse("""
+    description of the command.
+    
+    # Options
+    
+    - `-o, option=<value>`: some random option.
+    """)
+
+    @test_throws ErrorException split_docstring(content)
+end