Python Forum
argument and option scanning
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
argument and option scanning
#1
i am nearly finished with a new script that has a lot of code dedicated to argument and option scanning.  i think i need to write my own option scanning function(s).  existing tools like getopt just don't "cut the mustard".  one of the reasons is that i frequently use '-' and '+' and '--' and '++' for options, as well as embedded '=' and ':' for name=value option.  i also use multiple single letter options in the same argument as well as mixing '-' and '+' that way.  for example my rls command (implemented in C, some day to be re-implemented in Python) can be invoked to do exactly the same thing, 2 ways:
call( ['rls','+f','+l','-l','-p','foobar'] )
# or
call( ['rls','+fl-lp','foobar'] )
notice that the 'l' has a different meaning after '+' (select symlinks to be listed) compared to after '-' (lots of info,but not symlink reference). i want to make this library before doing 'rls' in Python.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can use argparse. Here is how to specify different prefix characters.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
here is what i want to do in the script i am currently working on.  there will be 3 positional arguments, name, number of columns, number of rows, followed by multiple arguments that make up a command to do in another context created by this script.  the name is mandatory while the number of columns and number of rows are optional.  the command is also optional.  name, columns, and rows may alternatively be given as named options in arbitrary order like (with example values):

--name=foobar

--cols=80

--rows=24

if the options are given then they are not to be given as positional arguments (the meaning by position shifts left).  the options can also be given as "two argument" assignments (in sys.argv they would come in using 2 positions in the list) like:

--name foobar

--cols 80

--rows 24

the -- part may be given as - or ++ or + or * or ** or / or // and this form may also be given in one argument:

name=foobar

cols=80

rows=24

the = may be given as : in any of the forms.  the option names may be abbreviated to 3, 2, or 1 character.  the option name may be given in either lower or upper or mixed case.  the value of the name option (or argument, if given that way) remains in the case (combination) it is given as. the value of the cols and rows option (or argument, if given that way) may be integer, float, decimal (default), octal, or hexadecimal.  the latter 2 value expressions need a notation indication the base.  float values may be given in business or scientific form (so cols=8E+1 and cols=80.0 give the same value).  any form that is valid in Python for int or float may be given, but the float values must be whole numbers.  the script will convert whatever it gets for these to int then to string which is how it uses them, after checking to be sure float values have no fractional part.  handling complex expressions correctly (whole number with the imaginary part always 0) is an implementation option.  if cols and/or rows is not given then there must be a way for the script to know this, such as a present or absent key in a dictionary,  the script does not need to know what form is used.

these are valid:

session foobar 80 24

session music play playlist/fav

session /c 166 /r 46 news newsfeed

session co=184 n=py3 ro=50 python3 somescript.py

these are invalid:

session (no name)

session +80-24 (invalid option, no name)

session ---co 80 +++n xyzzy ---ro 24

the script will be taking the existing terminal geometry (width and/or height) as defaults when columns and/or rows is not given.  getting these values is not expected of the argument parser.  if those cannot be obtained or are zero, then 80 and 24 will be used for columns and rows.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is python suitable for game map scanning? Securityfalcon 0 1,923 Mar-11-2019, 04:17 PM
Last Post: Securityfalcon

Forum Jump:

User Panel Messages

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