Python Forum
[solved] Basic question on list matchiing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] Basic question on list matchiing
#8
You could use ArgumentParser, which returns a Namespace, where you can access for example to answer.

from argparse import ArgumentParser


ANSWERS = ("n", "y")


def get_args():
    parser = ArgumentParser()
    parser.add_argument("--answer", dest="ANSWER", metavar="YOUR_CHOICE", default="n", choices=ANSWERS)
    return parser.parse_args()


if __name__ == "__main__":
    print(get_args())
I named the script c.py:
Quote:[andre@andre-Fujitsu-i5 ~]$ python c.py -h
usage: c.py [-h] [--answer YOUR_CHOICE]

options:
-h, --help show this help message and exit
--answer YOUR_CHOICE
[andre@andre-Fujitsu-i5 ~]$ python c.py --answer y
Namespace(ANSWER='y')
[andre@andre-Fujitsu-i5 ~]$ python c.py --answer i
usage: c.py [-h] [--answer YOUR_CHOICE]
c.py: error: argument --answer: invalid choice: 'i' (choose from 'y', 'n')

Also, arguments with more than one parameter (nargs) are possible.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Basic question on list matchiing - by snippsat - May-02-2022, 10:35 AM
RE: Basic question on list matchiing - by ndc85430 - May-02-2022, 10:54 AM
RE: Basic question on list matchiing - by paul18fr - May-02-2022, 11:51 AM
RE: [solved] Basic question on list matchiing - by DeaD_EyE - May-02-2022, 01:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list content check paul18fr 6 876 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  Basic Coding Question: Exit Program Command? RockBlok 3 676 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,724 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,243 May-11-2022, 02:23 PM
Last Post: menator01
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,386 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  Very basic calculator question BoudewijnFunke 4 2,037 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  [solved] Sort list paul18fr 5 2,982 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  basic question isinstance tames 5 2,933 Nov-23-2020, 07:20 AM
Last Post: tames
  basic question about tuples and immutability sudonym3 6 3,038 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  List index out of range error when attempting to make a basic shift code djwilson0495 4 3,102 Aug-16-2020, 08:56 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020