1

In order to parse some args via cmd line, I've a "switch case" written with if .. elif

if arg == 1:
    ..something...
elif arg >= 2 and arg < 4:
    ..something else ...
elif arg < 6:
    ..something else ...
else:
    ..something else ...

above, elif arg < 6: could be replaced by: elif arg == 4 or arg == 5:

this get's a bit messy when there are more argument values to check.

What could be the most efficient and easy way to read the code?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Sandt50
  • 19
  • 2
  • @jedwards The answers in the dup target just talks about checking if the values are equal. But that is not the case here, right? – thefourtheye Mar 13 '15 at 01:40
  • @thefourtheye The question talks about equal values, but there are good references in the answers that handle other situations. – jedwards Mar 13 '15 at 01:42
  • @jedwards May be I am not able to find the most appropriate one. Can you please share the answer which can work for this case? – thefourtheye Mar 13 '15 at 01:43
  • A switch statement certainly doesn't let you use arbitrary expressions like `arg >= 2`, so I don't understand how switch statements would even apply to your example code. – roippi Mar 13 '15 at 01:47
  • 1
    @thefourtheye, I missed the random `n<6`, which I assumed dealt with `arg` -- I'll nom for re-opening. – jedwards Mar 13 '15 at 01:50
  • 1
    @jedwards Its okay, I used the gold hammer to reopen :-) – thefourtheye Mar 13 '15 at 01:51
  • in the example `elif arg >= 2 and arg < 4:` just covers 2 conditions [2,3] that would be easily covered in a switch `case 2: case: 3: ...` – Sandt50 Mar 13 '15 at 01:52
  • @Sandt50 Should `n<6` be `arg<6`? How would you rewrite that as a `switch` -- are you switching on arg or n? – Two-Bit Alchemist Mar 13 '15 at 01:53
  • @Sandt50 FWIW `arg >= 2 and arg < 4` could be perhaps more cleanly written `2 <= arg < 4`. You could do some hacky stuff here with lambdas, which can be used as dict keys, and even build up an object that sort of behaves like a switch if you loop over it (one of the ActiveState recipes in the now removed dup target does just this), but at the end of the day, `if..elif..else` is going to be more readable. Unless there is some mathematical relationship you can exploit, you'll have the same number of cases, and even then the simplification could apply to both. – Two-Bit Alchemist Mar 13 '15 at 02:06
  • as it stands, `-1` would pass over the `if` and first `elif`, but would satisfy the `elif arg < 6` condition. is this intended? – the_constant Mar 13 '15 at 03:30

2 Answers2

2

You could try something like this:

def case_A():
    .. something ..

def case_B():
    .. something ..

def case_C():
    .. something ..

def case_else():
    .. something ..

{
    1: case_A,
    2: case_B,
    3: case_B,
    4: case_C,
    5: case_C
}.get(arg, case_else)()
David K. Hess
  • 16,632
  • 2
  • 49
  • 73
0

If your goal really is to parse a command line, use optparse if in python 2.6 or lower, argparse for later python versions. There is also a really nice little package called aaargh that makes for some really concise code. You will need to use decorators with that, so probably save that for a later project.

Use optparse or argparse, you can use things like -d somedir or --dir=somedir. It allows you to handle both optional parameters in any order, as well as required ones.

It also will generate a help message, if the command line does not meet your requirements.

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29