Questions tagged [argv]

Argument vector containing the arguments passed in from the command line when starting a program. Used in the main method, in conjunction with argc.

argv stands for arguments vector, and represents the arguments given to the program, or main, in a command-line. It is used in conjunction with , the number of arguments given to a program.

Keep in mind that the first value in argv contains the program name.

Example usage:

int main(int argc, char **argv) {
    /* Some code involving command-line arguments from argv */
}

Notable Stack Overflow questions:

1247 questions
30
votes
5 answers

Why does MPI_Init accept pointers to argc and argv?

this is how we use MPI_Init function int main(int argc, char **argv) { MPI_Init(&argc, &argv); … } why does MPI_Init use pointers to argc and argv instead of values of argv?
Rohit Banga
  • 18,458
  • 31
  • 113
  • 191
27
votes
3 answers

How to use python argparse with args other than sys.argv?

Is there a way to use argparse with any list of strings, instead of only with sys.argv? Here's my problem: I have a program which looks something like this: # This file is program1.py import argparse def main(argv): parser =…
ClydeTheGhost
  • 1,473
  • 2
  • 17
  • 31
26
votes
8 answers

How to change argv0 in bash so command shows up with different name in ps?

In a C program I can write argv[0] and the new name shows up in a ps listing. How can I do this in bash?
bstpierre
  • 30,042
  • 15
  • 70
  • 103
24
votes
7 answers

Python: Which encoding is used for processing sys.argv?

In what encoding are the elements of sys.argv, in Python? are they encoded with the sys.getdefaultencoding() encoding? sys.getdefaultencoding(): Return the name of the current default string encoding used by the Unicode implementation. PS: As…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
24
votes
6 answers

Using getopt in C with non-option arguments

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…
Conor Taylor
  • 2,998
  • 7
  • 37
  • 69
22
votes
5 answers

BASH scripting: n-th parameter of $@ when the index is a variable?

I want to retrieve the n-th parameter of $@ (the list of command line parameters passed to the script), where n is stored in a variable. I tried ${$n}. For example, I want to get the 2nd command line parameter of an invocation: ./my_script.sh alpha…
user438602
21
votes
8 answers

How to find the length of argv[] in C

#include #include #include int main(int argc, char *argv[]){ int fir; //badly named loop variable char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array for( fir = 1; fir<…
user1787351
  • 223
  • 1
  • 2
  • 5
20
votes
2 answers

Retrieve the command line arguments of the Python interpreter

Inspired by another question here, I would like to retrieve the Python interpreter's full command line in a portable way. That is, I want to get the original argv of the interpreter, not the sys.argv which excludes options to the interpreter itself…
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
20
votes
2 answers

python command line arguments in main, skip script name

This is my script def main(argv): if len(sys.argv)>1: for x in sys.argv: build(x) if __name__ == "__main__": main(sys.argv) so from the command line I write python myscript.py commandlineargument I want it to skip…
CQM
  • 42,592
  • 75
  • 224
  • 366
18
votes
7 answers

What is the type of command-line argument `argv` in C?

I'm reading a section from C Primer Plus about command-line argument argv and I'm having difficulty understanding this sentence. It says that, The program stores the command line strings in memory and stores the address of each string in an…
Jin
  • 1,902
  • 3
  • 15
  • 26
16
votes
3 answers

Argv - String into Integer

I'm pretty new at python and I've been playing with argv. I wrote this simple program here and getting an error that says : TypeError: %d format: a number is required, not str from sys import argv file_name, num1, num2 =…
Andrew Blanchette
  • 303
  • 1
  • 3
  • 7
16
votes
1 answer

Passing Argument 1 discards qualifiers from pointer target type

My main function is as follows: int main(int argc, char const *argv[]) { huffenc(argv[1]); return 0; } The compiler returns the warning: huffenc.c:76: warning: passing argument 1 of ‘huffenc’ discards qualifiers from pointer target…
Alex Nichols
  • 876
  • 5
  • 12
  • 23
15
votes
3 answers

Argument is URL or path

What is the standard practice in Python when I have a command-line application taking one argument which is URL to a web page or path to a HTML file somewhere on disk (only one) is sufficient the code? if "http://" in sys.argv[1]: print…
xralf
  • 3,312
  • 45
  • 129
  • 200
15
votes
4 answers

from sys import argv - what is the function of "script"

I am reading "Learn Python the Hard Way" and was confused by the "script" part of the second line. from sys import argv script, filename = argv From what I understand, the second line says: script and filename comprise argv. I tried running my code…
user1869775
  • 153
  • 1
  • 1
  • 4
14
votes
7 answers

How do you convert command line args in python to a dictionary?

I'm writing an application that takes arbitrary command line arguments, and then passes them onto a python function: $ myscript.py --arg1=1 --arg2=foobar --arg1=4 and then inside myscript.py: import sys argsdict = some_function(sys.argv) where…
priestc
  • 33,060
  • 24
  • 83
  • 117
1
2
3
83 84