Python Forum
i need a module for more involved command line parsing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need a module for more involved command line parsing
#11
i've done so much programming in assembly and C, where nothing to very little was available as tools (i did write a lot of my own) that doing things myself, without such tools, is just "second nature" to me. without argument parsing tools, i just wrote loops in a few hundred projects. with C there was getopt() and i started in C using it. but it could not do what i had already decided doing based on my experience with commands. even today, i write commands that "figure out" arguments and configuration "in context" rather than depend on lots of specific options, where i can. for example, in cloud programming, i have a big script that understands dozens of possible meanings of argument strings. if the string matches one of the region names, then i know a region was named. it does not need a "--region" option or need to be in a specific position. command line and config file parsing is something i have handled a lot and now can do very well. for Python, though, i have changed how i do config files: i read in the whole file, pass it to exec() with an empty dictionary, and use whatever it put in there.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#12
I know you have your own way of doing stuff,as can be a little annoying some times when you ask for advise and just ignore it anyway Wink

Maybe show a little more realistic example that i discuses in an older Thread.
This Linux command rm *.txt, *jpg using rm and delete all .bmp files and .jpg in a folder.
So let's do same with Click can call it py_rm.
# py_rm.py
from glob import iglob
from itertools import chain
import os
import click

def iter_glob(*args):
    return chain.from_iterable(iglob(pattern) for pattern in args)

@click.command()
@click.argument('arg', nargs=-1)
def rm(arg):
    for filename in iter_glob('*.txt', '*.bmp', '*jpg'):
         click.echo(f'File delete --> {filename}')
         os.remove(filename)

if __name__ == '__main__':
    rm()
Usage test:
Output:
λ py_rm.py *.txt *.jpg File delete --> field.txt File delete --> filename.txt File delete --> foo.txt File delete --> cb999.jpg File delete --> christmas-scene-greeting-card-27697787.jpg File delete --> jul1301.jpg File delete --> _MG_7951.jpg File delete --> _MG_7992.jpg
I think this is a quite elegant way of doing command line interfaces.
If you use Click,argparse,(or most likely write your own stuff),i do not care about Snooty

As for understanding decorator,look at my posts again.
All the magic is gone with this line.
make_bold = make_bold(foo) # The cooler way is @make_bold
make_bold = make_bold(foo) == @make_bold
By definition,a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.
With arguments it can soon get complex,but the basic is still the same.
Reply
#13
(Aug-28-2019, 05:56 PM)Skaperen Wrote:
(Aug-26-2019, 08:52 AM)wavic Wrote: Even argparse will do it.
can it handle the + options, too?


As a prefix? Yes, I think you can add any symbol you want?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
> By definition,a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.

i understand that. what i am still trying to understand is what kind of changes are done and what the decorator functions gets and can do to carry this out. one of the things it gets is a reference to the decorated function. the 2nd decoration in your code example at line 11 is @click.argument('arg', nargs=-1). where do those 2 "arguments" go? your decorated function is rm with one argument and you call it with no arguments. so how are the arguments manipulated? what if you did give an argument in the call to rm?

telling me that a decorator does make changes is not telling me how i could make use of the feature in my own programming, not just in following some example but also applying it to my own needs ... not just decorating functions i create but also writing decorator functions.

that and understanding the effect of multiple decorations and the difference between the 2 types of decorations in lines 10 and 11 of your code (the difference is arguments).

if i hand you a small gadget and you ask what it does and i say "just attach it to your car and it will save you 10% in the cost of gasoline" will you wonder how it works? then suppose you hang it from your dash window and discover your gas spending is 10% less ... will you then wonder even though you are going to leave it where it is?

i would wonder. maybe it is doing something illegal like linking to the gasoline pump an hacking in and changing the price for a while.

do you know how to write a decorator function?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#15
(Aug-30-2019, 03:59 AM)Skaperen Wrote: line 11 is @click.argument('arg', nargs=-1). where do those 2 "arguments" go?
All is good document in Click Variadic Arguments .
Click doc Wrote:This can be controlled with the nargs parameter. If it is set to -1, then an unlimited number of arguments is accepted.

skapren Wrote:telling me that a decorator does make changes is not telling me how i could make use of the feature in my own programming
If want to learn decorate then use tutorial/books it's a lot written about it,it's not the goal of Click to learn user decorator.
This is the goal,decorator help with "as little code as necessary" for users.
Click doc Wrote:Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary


Skaperen Wrote:do you know how to write a decorator function?
Did write one one in post #5,just as an easy example to understand.
Also if write a more robust version that meant to be used be other would also used @functools.wraps.
Or better wrapt.
This so metadata/signature of function eg __module__, __name__, __qualname__, __doc__, and __annotations__,don't get lost.

I am finish with this Thread now Hand ,the whole point was in post #2 to give a advice that i think Click is okay for these tasks.
Reply
#16
this still focused too narrow to learn about decorators, in general (a subtopic of this thread, now). focusing on just one possible decorator function does not tell me about the scope of what decorators do. do we need to do this in a new thread?

i have read about decorators, but everything i have read leaves more questions than answers. even the tutorial leave no place to ask. when i do see an email address i never get a reply.

the issue is the examples. they don't follow the scope explanations. for many topics, one example can be good enough. for many others, it is not. decorators is one that needs to at least start with a thorough scope explanation and possibly also a range one, as well. none of the tutorials went beyond just making a sales-pitch about decorators being the greatest thing since sliced bread (i hate sliced bread, really).

so far, i give up on decorators.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
I can some link on decorator that i think is good.
David Beazley Don't Repeat Yourself—Introducing Decorators | Decorators with Arguments.
SO Decorator Basics.
Primer on Python Decorators

Skaperen Wrote:do we need to do this in a new thread?
Yes if you have question about decorators make a new Thread,
this has nothing to do with you original question about command line parsing.
Reply
#18
the original question was indeed about command line parsing. i am currently writing a command that is not the original reason for asking, but it may be another case to consider because of the way it works.

it lists AMIs at AWS, either those that you created, or public ones. it takes a series of arguments that name AWS regions to be listed, or options to affect the command behavior. the options do not use "-" or "+" although i am thinking of adding that because people might type them in. if i do that, it might be to simply discard all non-alphanumeric characters until an alphanumeric one. so typing "--public" would be like typing "public" as would typing "/-=.,public". that would apply to typing regions, too. so "list_amis -us-east-2 //public ..eu-north-1 _ownerid" would be a valid command.

it's not complicated at all. a for loop scans sys.argv (after the command name was popped off) and each argument is checked to see if it is in the set of known regions (downloaded from AWS earlier) or in a dictionary of known options. if it is a region, it is appended to the list of regions. if it is an option, the name and value for an environment variable is obtained and set. all options are related to environment variables so that they can be preset and passed along when a new instance of itself is executed with subprocess.Popen().

if only one region is given, there are no child processes. if multiple regions are given, a child process runs the same command with just one region, for each region given (options are passed as environment variables).

to me, scanning the arguments for this is just too simple. i feel no parsing module is needed for this one.

the command that triggered the original question is a bit more complicated. in its C version i did not use getopt() because it would not handle it. and getopt() is the only one i found for C. but in Python, there are many choices. maybe one will work.

but i can't do debugging around decorators (yet) so that suggestion is outside of what i am prepared to do. i have to be able to debug my own code.

at this point i have a couple things active on my plate. one is too finish to program to list AWS AMIs. two is to go read those links about decorators.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#19
About decorators. Very well explained:
https://www.youtube.com/watch?v=FsAPt_9Bf3U
https://www.youtube.com/watch?v=KlBPCzcQNU8

I have never used click before. Just argparse and for my needs up until now, it's doing well.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#20
i took a quick look at that first video. i think i might be able to understand that after a few views. but that will have to wait until i have plenty of time.

i'm looking at argparse. how would you set up this?

a program can be run with either one argument or two arguments. if two arguments they must both be int or float. if float, the program needs the original fraction string so it can scale the number accurately (avoiding float rounding). what the program finally needs is both numbers scaled up by 10**18 as ints without losing any digits. negative values are not valid. decimal could do this, but the values must finally be int scaled up by 10**18.

if there is only one argument, it may be a time with a : character. the format could be one of these 3 (HH:MM:SS or HH:MM or :MM:SS). the missing units are made to be 0. HH must not be greater than 23. MM and SS must not be greater than 59. the units are only given as decimal even if there is a leading zero. more than two digits for any unit is invalid even if numerically it would be. negative values are not invalid.

so what kind of .add_argument() (or other) calls could do this with argparse?
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
  module either imported by a command or itself run as a command Skaperen 2 551 Dec-04-2023, 03:15 AM
Last Post: Skaperen
  review of command line parsers Skaperen 2 2,032 Mar-11-2021, 07:39 PM
Last Post: Skaperen
  command line options Skaperen 5 2,597 Aug-14-2020, 08:48 AM
Last Post: DeaD_EyE
  opening python from the command line takes a long time to load? abdulkaderanwar 4 2,901 Jun-22-2020, 03:42 AM
Last Post: abdulkaderanwar
  f-string in command line arguments Skaperen 0 1,546 May-05-2020, 11:49 PM
Last Post: Skaperen
  my own command line option parser Skaperen 0 1,641 Mar-27-2020, 04:14 AM
Last Post: Skaperen
  want suggested module for e-mail parsing Skaperen 0 1,483 Jul-26-2019, 08:52 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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