Bash: how to handle --options?

Need help with C, C++, perl, python, etc?

Bash: how to handle --options?

Postby Humbletech99 » 2006-08-31 16:15

I've tried to handle --options in the long gnu style format using the builtin getopts but it doesn't like it.

I know the external getopt does have an -l switch for this but the external is deprecated and I'd rather avoid using it.

I'm creating a wrapper script. What I need it to be able to take any number of arguments and then if a certain switch or two is passed, to tell the user off and exit 1. But I want everything to pass through to the program if one of the dangerous switches wasn't passed. So it needs to be able to not croak when a user passes --help to the underlying program.


Any ideas?
The Human Equation:

value(geeks) > value(mundanes)
User avatar
Humbletech99
 
Posts: 365
Joined: 2005-12-29 00:03

Postby njkt » 2006-09-02 16:44

User avatar
njkt
 
Posts: 39
Joined: 2006-07-29 08:43
Location: California

Postby ajdlinux » 2006-09-02 23:01

Yes, that link is extremely relevant for shell scripting...


On mod's request, I'll rephrase more nicely :) :

The link provided is for C programming and not really relevant to shell scripting. The way I would do it is in a post below.
Last edited by ajdlinux on 2006-09-04 07:04, edited 1 time in total.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby njkt » 2006-09-04 00:28

Nice use of sarcasm ;-)

My bad, but I like your suggestion too.
User avatar
njkt
 
Posts: 39
Joined: 2006-07-29 08:43
Location: California

Postby ajdlinux » 2006-09-04 06:56

The built in getopt is rather limited unfortunately.

In these instances I would use Python or another scripting language to do it in.

BTW, nice use of sarcasm ;)
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby ajdlinux » 2006-09-04 07:03

BTW, here's an example of processing this in Python:

Code: Select all
#!/usr/bin/python

import sys, string, os

dangerous_opts = string.find(sys.argv," --dangerous-option") or string.find(sys.argv,"-rf")

if not dangerous_opts:
    os.system("my_program "+sys.argv[1:])


Haven't tried this code yet but it should work.
Jabber: xmpp:ajdlinux@jabber.org.au
Spammers, email this: ajdspambucket@exemail.com.au
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby Humbletech99 » 2006-09-04 09:28

well, because what I'm doing is simple I've just parsed it myself, it's smaller and faster than python. Since this is just a wrapper, it needed to just disallow a couple of switches which is easier done just testing them quickly and bailing with an appropriate warning if found.

otherwise python would have been my next stop.

I'm sure that there is another way of doing this in shell, it's just that most people never get deep enough into it, I'll still keep an eye out for a better way to fully parse --long-options the way OptParse does for python.
The Human Equation:

value(geeks) > value(mundanes)
User avatar
Humbletech99
 
Posts: 365
Joined: 2005-12-29 00:03


Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable