733 questions
0
votes
1
answer
81
views
`/usr/share/doc/util-linux/getopt-example.bash`: when is `Internal error!` useful?
After finding out about getopt (and being disappointed it seems to have to work with eval no matter what, which means function inputs have to be filtered for security), I have nevertheless been using ...
3
votes
2
answers
107
views
Parsing a list of parameters built programmatically does not work with getopt
I am running a C program like this:
int main(int argc, char **argv)
{
int opt;
// Normal command line parsing
while ((opt = getopt(argc, argv, "s")) != -1)
printf("...
0
votes
0
answers
116
views
How to tokenize input string to POSIX and BASH compliant string array of arguments
I am trying to make a shell in C. I have made a argument tokenizer which takes a char * string and then should return a char ** string array with all the tokenized strings.
Doing this for simple ...
2
votes
1
answer
74
views
bash: -- being caught by first option, rather than by -- case
I have a script that parses command line arguments after using getopt. The issue is that the -- before my positional arguments is caught by my --usage) case, thus showing usage text erroneously. Is ...
4
votes
1
answer
231
views
catching missing argument from getopt
I'm trying to catch a missing argument when starting my program. This example is a simple file copy. the correct command line would be:
# prog -i in_file -o out_file
If the user forgets to enter an ...
0
votes
1
answer
62
views
Why can't assign value with getopt?
Here is my test.sh:
#!/usr/bin/env bash
arg="default"
TEMP=$(getopt --long been: -n 'test.sh' -- "$@")
eval set -- "$TEMP"
while true ; do
case "$1" in
...
1
vote
1
answer
160
views
What does the first colon mean in the ":b:" with getopt command line?
Show bash and getopt version in my OS:
bash --version |grep [r]elease
GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)
getopt --version
getopt from util-linux 2.38.1
In the getopt's manual(...
9
votes
1
answer
282
views
confusing param type of getopt
So, man page says getopt() takes char * const argv[], which is an array of constant pointers to char, as far as I understand. At the same time, getopt() permutes argv, so that eventually all non-...
2
votes
2
answers
143
views
Using getopt in C to handle colon-separated options like FFmpeg
I'm trying to implement an option handling similar to FFmpeg. Specifically, I want to support two types of palette options:
Direct palette specification: -p "255,0,0 0,255,0"
Config file ...
0
votes
1
answer
89
views
Why are digits as option "letters" considered "wrong" for getopt(3) on BSD and macOS? [closed]
In getopt(3) on both FreeBSD and macOS, it states (emphasis mine):
It is also possible to handle digits as option letters. This allows getopt() to be used with programs that expect a number ("-...
0
votes
0
answers
101
views
Why does getopt() not work when option and argument are separate?
I am trying to pass arguments to my process: (Product (int), Timestamp (std::string), LPN (std::string), Class (int) and State (int)).
I want to execute my process like this:
...
1
vote
1
answer
131
views
getopt() C function not working correctly in Ubuntu 23.10
This has been driving me crazy for a while cause it was working fine before on Ubuntu until I upgraded to Ubuntu 23.10. I'm calling the getopt() function in C without any loop and only once in the ...
1
vote
1
answer
215
views
Parsing Multiple Arguments with getopt()
I am working on a simple decimal to hex/binary calculator that will take command line arguments to specify which to convert to, -x for hex and -b for binary. I am using getopt() to parse in values.
I ...
2
votes
2
answers
110
views
Getopt::Long, Optional Arguments, and Greediness
I'm trying to write a program that has an option that takes an
optional argument, in such a way that it accepts options in the same
fashion with the exact same behavior as perl -i or git --color. ...
1
vote
2
answers
267
views
getopt optional parameters without the use of the format --name=value
I have getopt from util-linux 2.38 on my system:
[davide@68YM9C3 tmp]$ getopt -V
getopt from util-linux 2.38
[davide@68YM9C3 tmp]$
I would like to have an optional parameter to a long option but I ...
1
vote
1
answer
117
views
Is there a way to use getopt when flags arent at the beginning?
A C program I am writing takes files and flags as arguments. The program needs to support flags being placed anywhere amongst the arguments. So, for example, "./program file1 file2 -f 10 file3 ...
3
votes
3
answers
3k
views
Explain use of eval set -- "$ARGS" with getopt in Bash
I'm writing a simple Bash script that requires argument parsing and have composed the following script that uses getopt to parse short or long arguments provided to the script.
#!/bin/bash
# Default ...
0
votes
2
answers
160
views
Getopt sees extra '--' argument that I haven't included in the command
I'm trying to write some code that will tie together a series of conda tools and a bit of my own python code. I've provided some getopt options, but they parse weirdly. I'd like options to be able to ...
0
votes
1
answer
74
views
python Getopt- different behavior when I pass arguments using single-character command-line flags & equivalent longer command-line flags
Code:
import getopt
import sys
print("Let's read the arguments from command line")
user_inputs = sys.argv[1:]
print(user_inputs)
# get option and value pair from getopt
try:
opts, ...
0
votes
0
answers
99
views
getopt utility in macOS is not respecting or not allowing space character while passed with a string argument
getopt utility in macOS is not respecting or not allowing space character while passed with a string argument.
Below is sample script
#!/bin/bash
TEMP=$(getopt -n "$0" -a -l "project:&...
-2
votes
1
answer
68
views
Python can't parse command line arguments
I'm trying to get the arguments by using getopt
import sys
import getopt
import time
from datetime import timedelta
start_time = time.monotonic()
filename = ''
startIndex = 1
debug = False
outputFile =...
0
votes
0
answers
42
views
Calling a function that uses `getopt` inside another function that uses `getopt`
Consider the following script called mwe.sh:
#!/usr/bin/env bash
o() {
printf "DEBUG: o: executing...\n"
printf "DEBUG: o: args: %s\n" "$*"
local ...
0
votes
1
answer
159
views
getopt() in C doesn't read option arguments
I am typing up a program and it takes three command-line options: s, f and c
Since I was told to use getopt() as a function to get them, I looked into it.
The three options do the following:
s - this ...
0
votes
1
answer
197
views
getting an int from optarg
Im trying to get an int from the optarg from the c modifier in my code, but i can't get to it, I tried to cast it but it wasn't possible, so I investigated and found this atoi() function suposed to ...
0
votes
2
answers
98
views
Why I cant catch bad option when give unknown option to getopt_long?
So, I have next code:
#include<unistd.h>
#include<getopt.h>
#include<getopt.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
// #...
1
vote
1
answer
100
views
problem to make code that divides a large file (filename.txt) into smaller chunked files by lines in c
this is a code for chunk.c file which divides a large file (filename.txt) into smaller chunked files by lines.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <...
2
votes
1
answer
130
views
Running perl script and getting "Error in option spec:"
I am very new to running perl scripts and I am running into problems that I suspect are due to the assignment of variables.
Here is the beginning of the original script:
#!/usr/bin/perl -w
###########...
0
votes
0
answers
98
views
Why does strcmp always give 0
I build a doubly linked list which orders strings in alphabetical order as you add them. The strings come from an input file and I use getopt to get the input file from the command line. When i test ...
3
votes
1
answer
2k
views
getopt doesn't work as expected under MacOS with short options
I have the following command: command.sh bar -b=FOO
I'm trying to parse it with the following: getopt "m::b::" "$@"
Under Linux, the result is: -b =FOO -- command.sh bar
Under ...
0
votes
0
answers
51
views
How can I have getopt() support bundle of arguments and single character arguments
I want to be able to support commands such as:
./myProgram -f required_arg1 -v required_arg2
and
./myProgram -fv required_arg1 required_arg2
Where required_arg1 belongs to -f and required_arg2 ...
0
votes
1
answer
92
views
getopt_long setting optstring[0] to '+'
According to the man page for getopt and getopt_long, the GNU version reorders argv so that anything resembling a flag will be first, then it will return -1 when it reaches the first string that is ...
2
votes
1
answer
746
views
How does optind and argv behave in C?
I've been reading Head First C and I'm currently stuck at understanding int main(int argc, char *argv[]) and the optind variable of getopt(). I'm troubled by the same program as the one in How does &...
1
vote
1
answer
392
views
getopt returns 0 exit status for unrecognized option
The Unix man page for getopt states
If there are problems parsing the parameters, for example because a required argument is not found or an option is not recognized, an error will be reported on ...
0
votes
1
answer
53
views
Trying to set only long options in my Bash script but getting "Error parsing" message
I'm currently trying to set up my machine learning experiment to use Bash long options for Python arguments but am getting a parsing error. My script looks like this:
#! /usr/bin/bash
...
0
votes
1
answer
349
views
Make getopt optionally accept an argument without showing error
I'm trying to make the getopt to optionally accept an argument. For example, based on the code below:
#!/bin/bash
short_opts="e:"
options=$(getopt -o "${short_opts}" -- "$@&...
0
votes
0
answers
60
views
mandatory and optional options with argc and argv [duplicate]
I want to create a programme that will have mandatory option and optional option.
Mandatory:
-d <device>
Optional:
-h : help
-m <value> : launch an internal function with the value of ...
-1
votes
1
answer
99
views
Getopt doesn't seem to recognise arguments
I'm starting a new project in C++, I started by creating a function that handles command line arguments using getopt, but it doesn't seem to be working. I'm afraid it is a small mistake, but I've been ...
0
votes
0
answers
24
views
error occur when I use getopt() to create options in c programming, can someone tell me where did i done wrong
here is my code down below, the code is incomplete, but the problem is I have set the t as the only option I have for this program, and when i run it with option -t input, it gives me error as ...
0
votes
1
answer
826
views
how to use long options in c?
how to use long options via getopt_long:
for example like this:
--wide-option
I have --wide and -w.
And on --wide-option it gives the following error:
"unrecognized option"
int main(int ...
0
votes
0
answers
129
views
Parsing CLI arguments: switches vs. #!/usr/bin/env perl shebang (again)
My case at hand
An overgrown one-liner with 10 -sswitch-powered arguments,
that I desire to put into a file script whith /usr/bin/env-powered shebang.
10 arguments make me wish for a straightforward ...
0
votes
2
answers
89
views
how to limit the using of command line options in c using getopt()?
I am working on a c project, which
I am trying to write a program that allows users to use one of the command line options to invoke the program. For example
./program [-a or -b or -c] [-f filename] [-...
0
votes
1
answer
85
views
How to run bash script with getopt included in python?
I am running a script in the ubuntu terminal and it works fine.
./run_script2.sh -b ./exercises/13_caching.py
I want to run the same script in python os or subprocess but I am getting an error :
./...
0
votes
2
answers
179
views
Comparing chars from command line arguments in C
I am trying to compare options that can be entered on the command line for my program. for option f a user can enter c or b.
int main(int argc, char** argv){
char *i;
char *o;
char *f;
...
4
votes
1
answer
13k
views
Using getopt to parse command line options and their arguments
I am trying to use getopt to parse command line options passed by the user of the program. I am specifically struggling with how to save the "option arguments" that are passed with flag, for ...
0
votes
2
answers
344
views
BASH getopt with sub-arugments / sub-options & user input?
UPDATE:
After playing the with the code, the answer appears to be changing shift 1 ;; to shift 2 ;; below the input.
I'm not a BASH guru so I will leave a few days for someone to post a better answer.
...
0
votes
2
answers
874
views
Getopt not recognizing command line arguments (C program)
I am creating a C program in CentOS linux, and I cannot get my getopt to recognize arguments from the command line. I am relatively new to linux and C.
The error I get is 'command not found'
I ...
1
vote
1
answer
53
views
Getopt does not read repetitive argument
I am trying to get options via getopt.
I`ve run the program via the command line:
test.exe -ja 1:2 -ja 3:4
My code have following:
int c;
while ((c = getopt (argc, argv, "j:")) != -1) {
...
0
votes
1
answer
256
views
Python script short option filename argument is not recognized as an argument
I have a script that takes two arguments using sys and getopt module.
The issue I am having is that when using shortopts to test that the arguments are being passed correctly, the short option for the ...
0
votes
1
answer
85
views
Why optopt is 0? C
I don't understand why optopt is zero in my code when an option is recognized but is right when it goes to the '?'case. This is the code:
...
int opt;
while((opt = getopt(argc, argv, OPZIONI))!= -...
0
votes
1
answer
309
views
How to parse unordered flags in arguments via getopt in C
int res = 0;
while ((res = getopt(argc, argv, "ivlcne")) != -1) {
switch (res)
{
case 'i':
pCurrentFlags->i = 1;
break;
case 'v':
pCurrentFlags-&...