Ex4 - Learn More Python the Hard Way

So I’m stuck on the sys.argv part of this exercise. I can’t seem to figure out a smart way to look ahead to get the value of an optional argument.

Any suggestions on looking ahead while at say, --firstname to get its value

optargs_values = defaultdict(str)
optargs = {('-f', '--firstname'): 'firstname',
           ('-l', '--lastname'): 'lastname',
           ('-e', '--email'): 'email'}

files = []

def parse(args_ls):
    if help[0] in args_ls or help[1] in args_ls:
        for arg in args_ls:
            for (k,v) in optargs:
                if arg==k or arg==v:
                    next_value = #pass

So, first up, I’d say simplify your problem. Ditch the – style options and deal with just - style. Then, restrict it to options not flags, which means only the forms:

-f firstname

But not,


Turn on something.

Once you’ve simplified it down, realize that you have to just do this:

  1. Check for -, if not error.
  2. Get char after -, that’s option.
  3. Use if to figure out what that option should set.
  4. Take next param and that’s the option. Probably put those in a dict.
  5. Continue until you run out.

Once you have that working you can then add in the other stuff. Also, skip this help thing for now, again always simplify and get a working thing first, then add on to it.