Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with "getopt" module
#1
I really need help with the getopt module.
I'm trying to follow the "Blackhat Python" book and I am trying to make a Netcat replacement.
In it, I need to parse the command line arguments:
 opts, args = getopt.getopt(sys.argv[1:], #this part I dont understand) 
How do I use that part to my advantage? Please someone help!
Reply
#2
https://docs.python.org/2/library/getopt.html
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Sys.argv is a list which holds the all command line arguments.
Because its first element is the script's name the actual arguments start from index 1. [1:] part is called slicing. It means get the list elements from index 1 to the end.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
I would use the argparse module instead. It is much better. 
https://ttboj.wordpress.com/2010/02/03/g...-argparse/
Recommended Tutorials:
Reply
#5
Or even better Click,i have a run with Click here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (TypeError: 'module' object is not callable) for getopt Module nnevarez 2 2,865 Jul-03-2020, 01:45 AM
Last Post: nnevarez
  getopt with tuple not working proper Frank123456 0 1,866 Aug-21-2019, 12:46 PM
Last Post: Frank123456
  getopt not working right jdh239 1 4,417 Feb-28-2019, 05:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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