If you don't want to use dashes in front of your arg
and optional_argument
, that's kind of strange by typical Unix command-line behavior, but I don't understand why every answer appears to believe you have to use the dashes. Avoiding them is kind of trivial, actually...:
import sys
def before_and_after_doubledashes(args=sys.argv):
where_doubledashes = args.index('--') if '--' in args else len(args)
return args[:where_doubledashes], args[where_doubledashes+1:]
This completely ignores whether args start with dashes or not, just singles out the first occurrence of an arg that's exactly a double dash (if any) and returns a tuple of two lists, one all the args that are before that double dash if any, one all the args that are after it (empty if there's no double dash argument). You can assign these lists from the call:
before, after = before_and_after_doubledashes()
then treat them as you will (check their lengths, assign variables from part of them, etc).