getopt

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

微笑、不失礼 提交于 2019-12-01 02:27:12
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? getopt doesn't support what you are looking for. You can either use single-letter ( -a ) or long options ( --long ). Something like -ab is treated the same way as -a b : as option a with argument b . Note that

Is it possible to repeat getopt

£可爱£侵袭症+ 提交于 2019-12-01 01:28:09
问题 I'm trying to create a basic shell with builtin commands, and I'm having some issues with getopt. Here is the output (using valgrind): $ mkdir -p foo/bar mkdir -p foo/bar FLAGON $ mkdir -p foo/test mkdir -p foo/test ==15377== Invalid read of size 1 ==15377== at 0x5201BBE: _getopt_internal_r (in /usr/lib/libc-2.17.so) ==15377== by 0x5202CEA: _getopt_internal (in /usr/lib/libc-2.17.so) ==15377== by 0x5202D37: getopt (in /usr/lib/libc-2.17.so) ==15377== by 0x40351A: shell_ns_cmd_mkdir (shell.c

PHP getopt Operations

廉价感情. 提交于 2019-12-01 00:14:14
问题 This question is regarding getopt function in php. I need to pass two parameter to the php scripts like php script.php -f filename -t filetype Now depending upon the file type which can be u, c or s I need to do proper operation. I am using switch case for the same: Here is the Code I am using: // Get filename of input file when executing through command line. $file = getopt("f:t:"); Switch case should compare the type of the file which I am passing in from the command line (u, c or i) and

command line processing library - getopt

こ雲淡風輕ζ 提交于 2019-11-30 21:27:08
Can someone help me with the getopt function? When I do the following in main: char *argv1[] = {"testexec","-?"}; char *argv2[] = {"testexec","-m","arg1"}; int cOption; /* test for -? */ setvbuf(stdout,(char*)NULL,_IONBF,0); printf("\n argv1 "); while (( cOption = getopt (2, argv1, "m:t:n:fs?")) != -1) { switch(cOption){ case 'm': printf("\n -m Arg : %s \n",optarg); break; case '?': printf("\n -? Arg "); break; case 'n': printf("\n -n Arg : %s \n",optarg); break; } } printf("\n argv2 "); while (( cOption = getopt (3, argv2, "m:t:n:fs?")) != -1) { switch(cOption){ case 'm': printf("\n -m Arg :

using getopt with gdb

て烟熏妆下的殇ゞ 提交于 2019-11-30 21:10:49
have just incorporated getopt into my main() func getopt sets the global variable optarg for each call stepping through main() with gdb , after getopt() call optarg is always NULL (e.g. (gdb) p optarg ) yet printf("%s\n", optarg) outputs the cmd line arg as expected whats going on? why are the two not the same? Is this an isue with gdb and how it trys to inspect globals or is something else going on? Probably related: Bug 13800 - gdb does not print right values of getopt-related values Also notice ie: (gdb) n opt: 111, arg, 0x804a040 0x804a034 0x804a020 0x804a030 (gdb) printf "%p\n%p\n%p\n%p\n

Python之命令行参数

眉间皱痕 提交于 2019-11-30 16:14:20
命令行参数 很多程序可以执行一些操作来查看一些基本信息,Python 可以使用 -h 参数查看各参数帮助信息: $ python -h usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -c cmd : program passed in as string (terminates option list) -d : debug output from parser (also PYTHONDEBUG=x) -E : ignore environment variables (such as PYTHONPATH) -h : print this help message and exit [ etc. ] Python 提供了 getopt 模块来获取命令行参数。 $ python test.py arg1 arg2 arg3 Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表。 len(sys.argv) 是命令行参数个数。 注: sys.argv[0] 表示脚本名。 实例 test.py 文件代码如下: #

Is there a package to process command line options in R?

烈酒焚心 提交于 2019-11-30 15:30:08
Is there a package to process command-line options in R? I know commandArgs , but it's too basic. Its result is basically the equivalent to argc and argv in C , but I'd need something on top of that, just like boost::program_options in C++ , or GetOptions::Long in perl . In particular, I'd like to specify in advance what options are allowed and give an error message if the user specifies something else. The call would be like this (with user options --width=32 --file=foo.txt): R --vanilla --args --width=32 --file=foo.txt < myscript.R or, if Rscript is used: myscript.R --width=32 --file=foo.txt

How to get a value from optarg

佐手、 提交于 2019-11-30 12:19:53
问题 Hi I am writing a simple client-server program. In this program I have to use getopt() to get the port number and ip address like this: server -i 127.0.0.1 -p 10001 I do not know how can I get values from optarg, to use later in the program. 回答1: How about like this: char buf[BUFSIZE+1]; snprintf(buf,BUFSIZE,"%s",optarg); Or in a more complete example: #include <stdio.h> #include <unistd.h> #define BUFSIZE 16 int main( int argc, char **argv ) { char c; char port[BUFSIZE+1]; char addr[BUFSIZE

Using getopt in C with non-option arguments

天大地大妈咪最大 提交于 2019-11-30 06:52:53
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)atol(optarg); } case 'd': { d = (int)atol(optarg); } case 'b': buf = 1; break; case 't': time = 1;

Cross-platform getopt for a shell script

為{幸葍}努か 提交于 2019-11-30 05:16:59
I've just found out that getopt is not cross-platform (in particular for FreeBSD and Linux). What is the best workaround for this issue? Use getopts (with an "s"). According to Bash FAQ 35 : Unless it's the version from util-linux, and you use its advanced mode, never use getopt(1). getopt cannot handle empty arguments strings, or arguments with embedded whitespace. Please forget that it ever existed. The POSIX shell (and others) offer getopts which is safe to use instead. There are essentially two versions of the getopt command: the original version and the GNU enhanced version. The GNU