Julia Learner
Julia Learner

Reputation: 3032

What is the Julian way to combine positional, keyword, and default arguments and document them?

I am interested in a function to prompt the user for input with positional, keyword, and default arguments that is "Julian". I also want the documentation to be "Julian".

This example is what I have come up with so far:

"""
    ask([prompt::String="prompt> "] [kw_prompt::String=""])::String

Prompt user for input and read and return a string from `stdin`.

If keyword argument, `kw_prompt`, is supplied, it will be the prompt.

If positional argument, `prompt`, is supplied, it will be the prompt.

If no parameter is supplied, the prompt will be "prompt> ".

# Examples
```julia_repl
julia> ask()
prompt> test
"test"

julia> ask("My prompt: ")
My prompt: test
"test"

julia> ask(kw_prompt="A long prompt >>> ")
A long prompt >>> test
"test"
```
"""
function ask(prompt::String="prompt> "; kw_prompt::String="")::String
    if !isempty(kw_prompt)
        print(kw_prompt)
    elseif !isempty(prompt)
        print(prompt)
    end
    return readline()
end # ask()

Any suggestions as to either the code or the documentation?

Upvotes: 0

Views: 153

Answers (1)

mbauman
mbauman

Reputation: 31372

I would not call simultaneously supporting both positional and keyword args Julian. Just pick one.

If you really must, gloss over that detail in the documentation. Just chain the two together:

julia> """
           ask([prompt="prompt>"])
       """
       function ask(_prompt="prompt> "; prompt=_prompt)
           print(prompt)
           return readline()
       end
ask (generic function with 2 methods)

julia> ask();
prompt>

julia> ask("foo> ");
foo>

julia> ask(prompt="bar> ");
bar>

Upvotes: 3

Related Questions