2016-03-31 10 views

答えて

5

あなたは引数を解析された後の引数がparsed_args["argname"] == nothingとして設定された場合、あなたは(それがない設定されていた場合trueを返します)を確認することができます。

引数がないセット(ちょうど反対の行動のための!===を交換)した場合はtrueを出力します2つの引数で(見た目ArgParse.jlからexample1を修正)自己完結型の例を怒鳴る検索:

using ArgParse 

function main(args) 

    # initialize the settings (the description is for the help screen) 
    s = ArgParseSettings(description = "Example usage") 

    @add_arg_table s begin 
     "--opt1"    # an option (will take an argument) 
     "arg1"     # a positional argument 
    end 

    parsed_args = parse_args(s) # the result is a Dict{String,Any} 

    println(parsed_args["arg1"] == nothing) 
    println(parsed_args["opt1"] == nothing) 
end 

main(ARGS) 

および実施例コマンドラインコール(上記仮定test.jlに格納されている):

>>> julia test.jl 
true 
true 

>>> julia test.jl 5 
false 
true 

>>> julia test.jl 5 --opt1=6 
false 
false 

>>> julia test.jl --opt1=6 
true 
false 

ただし、パラメータが設定されているかどうかを確認するのではなく、デフォルト値を定義する方が適切な場合があります。これは、パラメータにdefaultキーワードを追加することによって行うことができます。それを導入するユーザーを強制する位置パラメータ、のためrequiredキーワードとして

@add_arg_table s begin 
    "--opt1" 
    "--opt2", "-o" 
     arg_type = Int 
     default = 0 
    "arg1" 
     required = true 
end 

aswell。

+0

引数にデフォルト値がある場合、渡されたかどうかを知ることができますか? – becko

+0

@beckoデフォルト値と比較することはできますが、ユーザーがデフォルト値を正確に導入すると、2つのケースを区別する方法が見つかりませんでした。 –

関連する問題