title: Argparse - different required parameters depnding on flag provided
body: <p>I wrote a tool in bash that I am trying to convert to python for the sake of expanding my skills beyond beginner but am hitting a snag due to the way I originally formatted the command.</p>
  
  <p>The tool restarts a service on a bunch of servers in a cluster that are usually named HOSTNAME01-08 so the original tool takes a hostname, cluster range, service, and action:</p>
  
  <p><code>$ prog.sh  hostname cluster_range httpd status (ex. of range would be 01-04)</code></p>
  
  <p>It also has a second function that will do a port check only</p>
  
  <p><code>$ prog.sh -p port_number hostname cluster_range</code></p>
  
  <p>The second commmand does not require the arguments of service and action. This is where I am hitting my issue.</p>
  
  <p>In converting it to python I am trying to use argparse for all the options and commands as I really like its ability to output clean usage and help information but the second function of the original tool is making this a bit harder.</p>
  
  <p>Using argparse if i set the '-p' flag that works fine and all, but its still requring the last two arguments that are not needed or it fails with "too few arguments"</p>
  
  <p>I thought maybe subparsers would be the way to go, but subparsers don't seem to work with flag style names (think i read somewhere it strips it?). The other issue is if I create the subparser for '-p' even with a proper name (say portcheck) it will only work if I create a subparser for the other task as well (call it main-task), it wont accept having base arguments for the main parser and separate arguments for just the subparser. So i would be forced to use commands like</p>
  
  <pre><code>$ prog.py  main-task  hostname  range  service  action
  $ prog.py  portcheck  port_num  hostname  range
  </code></pre>
  
  <p>Is there a way to use the optional '-p' flag and trigger it to not need the other two arguments? The best solution I can think of is to check for the -p flag and if true to set those two variables to empty strings, is it possible to adjust those arg values in an if statement? I could set those two arguments to default as empty to start with but then I couldn't error check if they don't provide all the arguments for the base function defeating the purpose of argparse. I originally started using sys.argv but was thinking argparse would be more pythonic and a good lesson to try as a beginner. Perhaps sys.argv may be the best way to go?</p>
  
  <p>argparse code I currently have:</p>
  
  <pre><code>parser = argparse.ArgumentParser(
      usage=None,
      formatter_class=argparse.RawDescriptionHelpFormatter,
      description=textwrap.dedent('''\
      Usage:
      drtt BASE-HOSTNAME  CLS-RANGE  SERVICE  ACTION
      ex. drtt HOSTNAME 01-08 sshd status
  
      For port check only:
      drtt [-p, --port-number] PORT-NUMBER  BASE-HOSTNAME  CLS-RANGE
      ex. drtt -p 1234 HOSTNAME 01-08'''))
  
  parser.add_argument('base_hostname', help='Host name only')
  parser.add_argument('--port-number', '-p', type=int, metavar='PORT-NUMBER', help="Port number to check if listening")
  parser.add_argument('cls_range', help='Number of hosts in cluster (written as start-finish ex. 01-16)')
  parser.add_argument('service', help='Service/Process to perform an action on')
  parser.add_argument('action', help='start, stop status or restart')
  args = parser.parse_args()
  </code></pre>
  
  <p>Sorry for the wordy post</p>