Advertisement
STANAANDREY

argparse ex

Aug 9th, 2024
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import argparse
  2.  
  3. # Initialize the main parser
  4. parser = argparse.ArgumentParser(description="Jira Command Line Interface")
  5.  
  6. # Add subparsers
  7. subparsers = parser.add_subparsers(dest="command")
  8.  
  9. # Add the 'status' subcommand
  10. status_parser = subparsers.add_parser("status", help="Check the status of a ticket")
  11.  
  12. # Create a mutually exclusive group for status subcommand
  13. status_group = status_parser.add_mutually_exclusive_group(required=True)
  14. status_group.add_argument("--list", dest="tickets", action="store_const", const="list", help="List tickets")
  15. status_group.add_argument("--update", dest="tickets", action="store_const", const="update", help="Update a ticket")
  16. status_group.add_argument("--transitions", dest="tickets", action="store_const", const="transitions", help="View ticket transitions")
  17.  
  18. # Add a positional argument for the ticket number
  19. status_parser.add_argument("ticket_nr", metavar="TICKET", type=str, help="The ticket number")
  20.  
  21. # Parse the arguments
  22. args = parser.parse_args()
  23.  
  24. # Now, you can access both ticket_nr and the chosen action
  25. if args.command == "status":
  26.     print(f"Action: {args.tickets}, Ticket Number: {args.ticket_nr}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement