• <i id='xmDMD'><tr id='xmDMD'><dt id='xmDMD'><q id='xmDMD'><span id='xmDMD'><b id='xmDMD'><form id='xmDMD'><ins id='xmDMD'></ins><ul id='xmDMD'></ul><sub id='xmDMD'></sub></form><legend id='xmDMD'></legend><bdo id='xmDMD'><pre id='xmDMD'><center id='xmDMD'></center></pre></bdo></b><th id='xmDMD'></th></span></q></dt></tr></i><div id='xmDMD'><tfoot id='xmDMD'></tfoot><dl id='xmDMD'><fieldset id='xmDMD'></fieldset></dl></div>
      <bdo id='xmDMD'></bdo><ul id='xmDMD'></ul>

    <tfoot id='xmDMD'></tfoot>
  • <small id='xmDMD'></small><noframes id='xmDMD'>

      1. <legend id='xmDMD'><style id='xmDMD'><dir id='xmDMD'><q id='xmDMD'></q></dir></style></legend>

        Python 命令参数

        时间:2023-09-28

          <small id='CBq4x'></small><noframes id='CBq4x'>

            <tbody id='CBq4x'></tbody>
          <i id='CBq4x'><tr id='CBq4x'><dt id='CBq4x'><q id='CBq4x'><span id='CBq4x'><b id='CBq4x'><form id='CBq4x'><ins id='CBq4x'></ins><ul id='CBq4x'></ul><sub id='CBq4x'></sub></form><legend id='CBq4x'></legend><bdo id='CBq4x'><pre id='CBq4x'><center id='CBq4x'></center></pre></bdo></b><th id='CBq4x'></th></span></q></dt></tr></i><div id='CBq4x'><tfoot id='CBq4x'></tfoot><dl id='CBq4x'><fieldset id='CBq4x'></fieldset></dl></div>
            • <bdo id='CBq4x'></bdo><ul id='CBq4x'></ul>

              <tfoot id='CBq4x'></tfoot>

            • <legend id='CBq4x'><style id='CBq4x'><dir id='CBq4x'><q id='CBq4x'></q></dir></style></legend>

                1. 本文介绍了Python 命令参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经在谷歌上搜索了将近一个小时,但被卡住了.

                  I have been googling almost an hour and am just stuck.

                  对于一个脚本,stupidadder.py,它将 2 添加到命令 arg.

                  for a script, stupidadder.py, that adds 2 to the command arg.

                  例如python 愚蠢的adder.py 4

                  e.g. python stupidadder.py 4

                  打印 6

                  python愚蠢的adder.py 12

                  python stupidadder.py 12

                  打印 14

                  到目前为止我已经用谷歌搜索过:

                  I have googled so far:

                  import argparse
                  parser = argparse.ArgumentParser(description='Process some integers.')
                  parser.add_argument('x', metavar='x', type=int, nargs='+',
                                      help='input number')
                  
                  ...
                  
                  args = parser.parse_args()
                  print args
                  x = args['x']  # fails here, not sure what to put
                  print x + 2
                  

                  我在任何地方都找不到直接的答案.文档太混乱了.:( 有人可以帮忙吗?请,谢谢.:)

                  I can't find a straightforward answer to this anywhere. the documentation is so confusing. :( Can someone help? Please and thank you. :)

                  推荐答案

                  我不完全确定你的目标是什么.但如果这就是你所要做的一切,你不必变得非常复杂:

                  I'm not entirely sure what your goal is. But if that's literally all you have to do, you don't have to get very complicated:

                  import sys
                  print int(sys.argv[1]) + 2
                  

                  这里是一样的,但有一些更好的错误检查:

                  Here is the same but with some nicer error checking:

                  import sys
                  
                  if len(sys.argv) < 2:
                      print "Usage: %s <integer>" % sys.argv[0]
                      sys.exit(1)
                  
                  try:
                      x = int(sys.argv[1])
                  except ValueError:
                      print "Usage: %s <integer>" % sys.argv[0]
                      sys.exit(1)
                  
                  print x + 2
                  

                  示例用法:

                  C:Usersuser>python blah.py
                  Usage: blah.py <integer>
                  
                  C:Usersuser>python blah.py ffx
                  Usage: blah.py <integer>
                  
                  C:Usersuser>python blah.py 17
                  19
                  

                  这篇关于Python 命令参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:TypeError: __init__() 至少需要 4 个非关键字参数(给定 3 个) 下一篇:提供可变数量参数的python函数

                  相关文章

                  • <bdo id='7eatn'></bdo><ul id='7eatn'></ul>
                  1. <small id='7eatn'></small><noframes id='7eatn'>

                    <i id='7eatn'><tr id='7eatn'><dt id='7eatn'><q id='7eatn'><span id='7eatn'><b id='7eatn'><form id='7eatn'><ins id='7eatn'></ins><ul id='7eatn'></ul><sub id='7eatn'></sub></form><legend id='7eatn'></legend><bdo id='7eatn'><pre id='7eatn'><center id='7eatn'></center></pre></bdo></b><th id='7eatn'></th></span></q></dt></tr></i><div id='7eatn'><tfoot id='7eatn'></tfoot><dl id='7eatn'><fieldset id='7eatn'></fieldset></dl></div>

                    <tfoot id='7eatn'></tfoot>

                    1. <legend id='7eatn'><style id='7eatn'><dir id='7eatn'><q id='7eatn'></q></dir></style></legend>