getopt

getopt switch statement never hitting default case

匆匆过客 提交于 2020-01-03 04:07:31
问题 I've tried searching and haven't found a duplicate/similar question. How come "default" is never triggering in this case? Working on some previous homework assignments for a course to prepare for the fall, and I'm having an issue with getopt() in C. Here are the particular lines in question: while ((c = getopt(argc, argv, "i:o:")) != -1) { switch (c) { case 'i': inFile = strdup(optarg); break; case 'o': outFile = strdup(optarg); break; default: usage(); //this prints the "usage" statement and

Perl Getopt Using Same Option Multiple Times

本秂侑毒 提交于 2019-12-31 04:28:09
问题 In Perl getopts , is it possible to use the same option multiple times but with different values ? I want to give the user the option of entering different grid coordinates but uses the same option name to minimize confusion. Ex: my_grid.pl --coords=10,12 --coords=-18,30 --coords=4,-25 The script would then perform a set of actions on those different pairs. There will always be at least one pair but there is no knowing how many pairs from situation to situation. I would like to avoid: -

How to make a multi-character parameter in UNIX using getopt?

折月煮酒 提交于 2019-12-30 07:56:29
问题 I'm trying to make a getopt command such that when I pass the "-ab" parameter to a script, that script will treat -ab as a single parameter. #!/bin/sh args=`getopt "ab":fc:d $*` set -- $args for i in $args do case "$i" in -ab) shift;echo "You typed ab $1.";shift;; -c) shift;echo "You typed a c $1";shift;; esac done However, this does not seem to work. Can anyone offer any assistance? 回答1: getopt doesn't support what you are looking for. You can either use single-letter ( -a ) or long options

How to make a multi-character parameter in UNIX using getopt?

不想你离开。 提交于 2019-12-30 07:56:29
问题 I'm trying to make a getopt command such that when I pass the "-ab" parameter to a script, that script will treat -ab as a single parameter. #!/bin/sh args=`getopt "ab":fc:d $*` set -- $args for i in $args do case "$i" in -ab) shift;echo "You typed ab $1.";shift;; -c) shift;echo "You typed a c $1";shift;; esac done However, this does not seem to work. Can anyone offer any assistance? 回答1: getopt doesn't support what you are looking for. You can either use single-letter ( -a ) or long options

Using getopt in C with non-option arguments

别等时光非礼了梦想. 提交于 2019-12-30 01:56:07
问题 I'm making a small program in C that deals with a lot of command line arguments, so I decided to use getopt to sort them for me. However, I want two non-option arguments (source and destination files) to be mandatory, so you have to have them as arguments while calling the program, even if there's no flags or other arguments. Here's a simplified version of what I have to handle the arguments with flags: while ((c = getopt(argc, argv, "i:d:btw:h:s:")) != -1) { switch (c) { case 'i': { i = (int

How can I allow undefined options when parsing args with Getopt

十年热恋 提交于 2019-12-30 00:53:11
问题 If I have a command line like: my_script.pl -foo -WHATEVER My script knows about --foo , and I want Getopt to set variable $opt_foo , but I don't know anything about -WHATEVER . How can I tell Getopt to parse out the options that I've told it about, and then get the rest of the arguments in a string variable or a list? An example: use strict; use warnings; use Getopt::Long; my $foo; GetOptions('foo' => \$foo); print 'remaining options: ', @ARGV; Then, issuing perl getopttest.pl -foo -WHATEVER

Am I understanding getopt() correctly?

五迷三道 提交于 2019-12-25 18:53:07
问题 I'm trying to scan the command line for certain letters, symbols and values. I want to scan for "-w" , a number, and "-s" . I got a response in my last question, I was told to use getopt() and after a little bit of Googling, I think I might get it, but I'm not sure. This is what I think I'm doing: int c = 0; int b = argv[2]; while((c = getopt(argc, argv, "-w", "-s", b)) I think I'm scanning argc for "-w" , "-s" and the argv[2] value (which is the number). But I do not know if I am using it

C Using a file argument with GetOpt

佐手、 提交于 2019-12-25 00:53:20
问题 Is there a better way to find the name of a file than just looping through argv[] looking for an argument that isn't a flag - ? In this case, the flags can be entered in any order (so optind isn't going to help). ie: /program -p file.txt -s /program -p -s file.txt -b /program file.txt -p -s -a int main (int argc, char *argv[]){ char option; const char *optstring; optstring = "rs:pih"; while ((option = getopt(argc, argv, optstring)) != EOF) { switch (option) { case 'r': //do something break;

The simplest possible getopt program I can get?

扶醉桌前 提交于 2019-12-24 13:24:04
问题 After doing some reading on this link on how to use getopt() , I'm trying to get a small example. What I want, is something like: ./prog -v # show me prog version ./prog -f filename # just show me the filename I entered from the command line Here is what I wrote so far: #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, *argv[]) { char VER[] = "0.1.1"; int opt; opt = getopt(argc, argv, "vf:"); char *filename; while (opt != -1) { switch(opt) { case 'v': printf(

How do I correctly use getopt in a cshell script?

随声附和 提交于 2019-12-24 12:34:51
问题 Problem I am trying to make a chshell script that takes in user input from several options. Inspired by this bash example, I tried to construct a while loop and switch case statement pattern around the getopt command. After several iterations I have tried to interpret this example from IBM: set argv=`getopt :a:b:c:d:e: $*` if ($? != 0) then exit 1 endif while ({ ` $1 != --` }) switch ($1) case[-a]: yada breaksw case[-b]: yada breaksw case[-c]: yada breaksw case[-d]: yada breaksw case[-e]: