getopt

getopt how to set a default value

主宰稳场 提交于 2019-12-11 04:08:39
问题 #include <stdio.h> #include <stdlib.h> #include <getopt.h> #include <string.h> int main(int argc, char **argv) { int o; int w = 10; while ((o = getopt(argc, argv, "w")) != -1) { switch (o) { case 'w' : w = atoi(optarg); break; } } printf("%d\n", w); } Basically, I want -w to have if nothing is inputted. Consider these use cases $ gcc -Wall fileabove.c $ ./a.out 10 $ ./a.out -w 10 $ ./a.out -w14 14 I can't get the second one to work. Is there any way I can play around with getopt to get the

Pass BASH associative arrays to PHP script

此生再无相见时 提交于 2019-12-11 03:35:16
问题 Is it possible to pass BASH associative arrays as argv to PHP scripts? I have a bash script, that collects some variables to a bash associative array like this. After that, I need to send it to PHP script: typeset -A DATA DATA[foo]=$(some_bash_function "param1" "param2") DATA[bar]=$(some_other_bash_function) php script.php --data ${DATA[@]} From PHP script, i need to access the array in following manner: <?php $vars = getopt("",array( "data:" )); $data = $vars['data']; foreach ($data as $k=>

getopt usage with/without option

时间秒杀一切 提交于 2019-12-11 03:12:08
问题 I'm writing a simple code making use of *argv[] parameter. I'd like to know whether I can use getopt() function for the following intent. ./myprogram -a PATH ./myprogram PATH The program can either take merely PATH (e.g. /usr/tmp ) or take -a option in addition to PATH . Can getopt() be used for this state? If can, how? 回答1: The program can either take merely PATH (e.g. /usr/tmp ) or take option in addition to PATH . Can getopt() be used for this state? If can, how? Certainly. I'm not sure

How to get Getopt::Long + pod2usage working?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 02:48:37
问题 I'm about to go nuts so here I am :) I'm trying to make documentation for my Perl program, but I never manage to get Getopt::Long and pod2man working. Here is a simple program I wrote for testing purpose: #!/usr/bin/perl use strict; use warnings; use Getopt::Long; use Pod::Usage; Getopt::Long::Configure ("bundling"); my $option = ""; my $verbose = 1; my $quiet = 0; my $man = 0; my $help = 0; GetOptions ( "option=s" => \$option, "verbose" => sub { $verbose = 1; $quiet = 0 }, "quiet|noverbose"

Put operands first in getopt()

半城伤御伤魂 提交于 2019-12-11 02:42:53
问题 Using the getopt() function in C, it is possible to do that: program -a arg_for_a -b arg_for_b -c operand1 operand2 and it works with no problem. But, how to make it work this way?: program operand1 operand2 -a arg_for_a -b arg_for_b -c In this case, every argument, including the -a , -b etc. are considered to be operands. I'm trying to make just like like gcc or ssh does: gcc code.c -o executable ssh user@host -i file.pem That is, no matter in what position the options and the operands come,

Bash getopt - shift multiple parameters

ε祈祈猫儿з 提交于 2019-12-11 02:13:36
问题 I have a script to launch some checks on Ubuntu machine. I call my script like this : ./script -f /tmp/file.txt --modules 001 002 003 All files 001, 002, etc... are bash scripts. The following main function is my problem : main () { OPTS=`getopt -o hf:m: --long help,file:,modules: -n 'script.sh' -- "$@"` eval set -- "$OPTS" [ $# -eq 0 ] && echo "Unknown options or parameters" && USAGE while [ $# -gt 0 ]; do case "$1" in -f|--file) FILE="$2" shift ;; -h|--help) USAGE exit 1 ;; -m|--modules)

Bash long options/flags - how to do it?

こ雲淡風輕ζ 提交于 2019-12-10 19:29:36
问题 I am trying to change my working script with getopts to getopt ( long flags ). Below i present my code which is working. getopts 'm:' mode modeValue=$OPTARG getopts 'p:' parameter parameterValue=$OPTARG getopts 'u:' parameter parameterValue2=$OPTARG getopts 'l:' parameter parameterValue3=$OPTARG getopts 'n:' parameter parameterValue4=$OPTARG getopts 'e:' parameter parameterValue5=$OPTARG getopts 'w:' parameter parameterValue6=$OPTARG getopts 'r:' parameter parameterValue7=$OPTARG case

How to allow non-option arguments in any order to getopt?

a 夏天 提交于 2019-12-10 15:36:56
问题 I have a C program which expects to be called with several options and 1 non-option argument (i.e. with no associated option letter), and uses getopt to parse these options. For example, it could be called with: Example1: myProgram -a "aParam" -b "bParam" "xParam" I have been using SLES10, and the options worked in any order. For example, the non-option argument, "xParam" could come first: Example2: myProgram "xParam" -a "aParam" -b "bParam" However, when testing in SLES11, it seems that

python超链接抓取工具

人盡茶涼 提交于 2019-12-10 12:44:36
python实现自动抓取某站点内所有超链接 (仅供学习使用) 代码部分 #!/usr/bin/python import requests import time import re import sys, getopt #命令行选项 from bs4 import BeautifulSoup localtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) #时间 z=[] #存取网站 x=[] #优化网站,去除冗杂部分 def main(argv): url = '' #输入的网址 file_path = '' #保存路径 try: opts, args = getopt.getopt(argv,"hu:f:",["url=","file="]) except getopt.GetoptError: print ('allsite.py -u <url> -f <file>') sys.exit(2) for opt, arg in opts: if opt == '-h': #帮助 print ('allsite.py -u <url> -f <file>') sys.exit() elif opt in ("-u", "--url"): #输入网址 url = arg re1 = requests.get(url)

Can Python's argparse permute argument order like gnu getopt?

不打扰是莪最后的温柔 提交于 2019-12-10 03:28:58
问题 GNU getopt, and command line tools that use it, allow options and arguments to be interleaved, known as permuting options (see http://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html#Using-Getopt). Perl's Getopt::Long module also supports this (with qw(:config gnu_getopt)). argparse seems to not support (or even mention) permuting options. There are many SO questions related to arg/opt order, but none seem answer this question: Can argparse be made to permute argument order like