Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any function in java which behaves like getopt from c

Tags:

java

Hello I am working on command line application which can accepts command line argument like

app -port 8888 -filename d:\xyz\xyz.pdf -dest d:\pqr

I am looking for function which can return me pair of option and it corresponding value like getopt in c.

like image 370
Prasad S Deshpande Avatar asked Aug 02 '12 18:08

Prasad S Deshpande


People also ask

Is getopt standard C?

getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems.

What is getopt function in C?

The getopt() is one of the built-in C function that are used for taking the command line options. The syntax of this function is like below − getopt(int argc, char *const argv[], const char *optstring) The opstring is a list of characters. Each of them representing a single character option.

How do I use getopt?

Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option. Return Value: The getopt() function returns different values: If the option takes a value, that value is pointer to the external variable optarg.

What does getopt return?

RETURN VALUE The getopt() function returns the next option character specified on the command line. A colon (:) is returned if getopt() detects a missing argument and the first character of optstring was a colon (:).

What is getopt in C with example?

getopt() function in C to parse command line arguments. The getopt() function is a builtin function in C and is used to parse command line arguments. Syntax: getopt(int argc, char *const argv[], const char *optstring) optstring is simply a list of characters, each representing a single character option.

How to parse command line arguments using getopt in C?

getopt () function in C to parse command line arguments. The getopt () is one of the built-in C function that are used for taking the command line options. The syntax of this function is like below −. The opstring is a list of characters.

How does GNU getopt () work?

In GNU getopt (), the default behavior is to allow options to appear anywhere on the command line. The getopt () method permutes the argument to make it appear to the caller that all options were at the beginning of the command line, and all non-options were at the end.

What is the output of getopt in Python?

If getopt () comes across an option character that is not in ‘optstring’, it will return the (‘?’) character as an output. If an argument is missing as an option, it should return a colon (‘:’) as an output.


1 Answers

There are quite a few command line parser libraries for Java out there.

A recent (as of late 2019) command line parser with a lot of momentum and features is picocli.

Some older, popular ones are commons-cli (quite old), args4j and JCommander.

like image 98
Stefan Ferstl Avatar answered Oct 15 '22 10:10

Stefan Ferstl