LoginSignup
1
0

pythonで雑に実行引数の処理をする

Posted at

雑に実行時引数の処理をする

雑に引数処理します、辞書型のある言語なら同じロジックが使えます
引数の解説だとかヘルプだとかバリデーションとか無いですが機能します
一般的な引数パースのライブラリがあるので使う機会あまり無いと思いますが


# argparse.py
import sys

def argparse(arguments, options):
    keyValue = ""
    keyReaded = False
    for argument in arguments:
        trimedArgument = argument.strip()
        if keyReaded == False:
            if trimedArgument in options:
                keyValue = trimedArgument
                keyReaded = True
        else:
            options[keyValue] = trimedArgument
            keyReaded = False
    return options

def test():
    options = {
        "-a": "a-default-value",
        "-b": "b-default-value",
        "-c": "c-default-value",
    }
    parsedOptions = argparse(sys.argv, options)
    print("parsedOptions", parsedOptions)
    

if __name__ == "__main__":
    test()

1
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0