{"id":4824,"date":"2025-01-22T16:14:09","date_gmt":"2025-01-22T16:14:09","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4824"},"modified":"2025-05-16T19:33:12","modified_gmt":"2025-05-16T18:33:12","slug":"getting-command-line-arguments","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/application-control\/getting-command-line-arguments","title":{"rendered":"Getting command line arguments"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Get arguments<\/h4>\n\n\n\n<p>Returned as a list, index 0 being the path + filename of the python file that has been run<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    import sys\n    command_line_arguments = sys.argv\n    #print(\"This is the name of the program:\", sys.argv&#91;0])\n    #print(command_line_arguments)            #E.g. &#91;'C:\\\\MyFolder\\\\my_app.py', 'abc', 'xyz]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get any arguments excluding the default index0 path + filename of the python file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>    #Get any arguments\n    command_line_arguments = parse_arguments(sys.argv&#91;1:])<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get argument and its value<\/h4>\n\n\n\n<p>This example forces to lower case, remove .lower() if you don&#8217;t want that<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    found_it = False\n    for argument in command_line_arguments:\n        if (argument.lower().find(\"my_argument_text=\") == 0):        #0=does string start with, -1=not found\n                found_it = True\n                argument_value = argument&#91;argument.lower().index(\"my_argument_text=\")+17:]\n                break<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Handling key pair arguments<\/h4>\n\n\n\n<p>E.g. arg1=&#8221;True&#8221; arg2=1234<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def parse_arguments(args):\n    parsed_args = {}\n    for arg in args:\n        if '=' in arg:\n            key, value = arg.split('=', 1)\n            value = value.strip('\"')        #Remove any quote marks around a string\n            parsed_args&#91;key] = value\n        else:\n            parsed_args&#91;arg] = ''\n    return parsed_args\n\ndef main():\n    #Get any arguments\n    command_line_arguments = parse_arguments(sys.argv&#91;1:])\n\n    if 'my_argument_name' in command_line_arguments:\n        my_argument_name_= int(command_line_arguments&#91;'some_argument_name'])\n\n    if command_line_arguments.get('arg1', '').lower() == 'true':\n        #Do something...<\/code><\/pre>\n\n\n\n<p>Testing arguments when developing<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEBUG_RUN_ARGUMENTS = &#91;'', 'my_argument_a', 'my_argument_b=abc', ]\n#DEBUG_RUN_ARGUMENTS = &#91;]\n\n    if (len(DEBUG_RUN_ARGUMENTS) &gt; 0):          #Overide with debug arguments\n        command_line_arguments = parse_arguments(DEBUG_RUN_ARGUMENTS&#91;1:])<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Get arguments Returned as a list, index 0 being the path + filename of the python file that has been run Get any arguments excluding the default index0 path + filename of the python file Get argument and its value This example forces to lower case, remove .lower() if you don&#8217;t want that Handling key [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[345],"tags":[],"class_list":["post-4824","post","type-post","status-publish","format-standard","hentry","category-application-control"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4824"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4824\/revisions"}],"predecessor-version":[{"id":4943,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4824\/revisions\/4943"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}