0
\$\begingroup\$

This is a follow up to Log probe requests of WiFi devices focussing on a specific element of the code.

How can I indent this code to make it look great and be well formatted?

parser = argparse.ArgumentParser(description='Collect WiFi probe requests')
parser.add_argument('-i', '--interface',
                    default=default_interface,
                    help='the interface used for monitoring')
parser.add_argument('--tshark-path',
                    default=distutils.spawn.find_executable("tshark"), 
                    help='path to tshark binary')
parser.add_argument('--ifconfig-path',
                    default=distutils.spawn.find_executable("ifconfig"), 
                    help='path to ifconfig')
parser.add_argument('--iwconfig-path',
                    default=distutils.spawn.find_executable("iwconfig"), 
                    help='path to iwconfig')
parser.add_argument('-o', '--output',
                    default='-', 
                    help='output file (path or - for stdout)')
parser.add_argument('-c', '--channel',
                    default='all', 
                    help='channel/s to hop (i.e. 3 or 3,6,9 or 3-14 or all or 0 for current channel')
parser.add_argument('--verbose',
                    action='store_true', 
                    help='verbose information')
parser.add_argument('-p', '--only-probes',
                    action='store_true',
                    help='only saves probe data spit by newline')
parser.add_argument('--delay',
                    default=5,
                    help='delay between channel change')
args = parser.parse_args()
\$\endgroup\$
1
\$\begingroup\$

Python has a style guide, which lays out guidelines for (among other things) code formatting. Your current code is compliant with the style guide, except that the help parameter for --channel means the line is too long.

You can easily avoid this by simply breaking the string across multiple lines:

parser.add_argument('-c', '--channel',
                    default='all',
                    help='channel/s to hop (i.e. 3 or 3,6,9 or 3-14 or all or 0 '
                    'for current channel')

Alternatively, you can shorten the message slightly and use one of the other recommended indent styles, which uses less horizontal space:

parser.add_argument(
    '-c', '--channel',
    default='all',
    help="channel/s to hop (e.g. '3', '3,6,9', '3-14', 'all', '0' (current channel)")
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Ok. Should I put a blank line between entries? Without it seems to me a little bit confusing. \$\endgroup\$ – 0x2b3bfa0 Mar 13 '15 at 14:18
  • 1
    \$\begingroup\$ @Helio the style guide says blank lines should be used "sparingly". I think the blank space at the start of the parameter lines between each parser... is clear enough, but you can add them if you like. \$\endgroup\$ – jonrsharpe Mar 13 '15 at 14:19
  • \$\begingroup\$ Ok, I used the second option without putting newlines. Thank you very much! \$\endgroup\$ – 0x2b3bfa0 Mar 13 '15 at 14:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.