gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

sys.argv[1] with wiringPI

Sun Feb 28, 2016 10:22 am

Hi!
I started using wiringPi for UART for the raspberry pi and my attiny2313.
I don't want to write ten 1 character different .c code.
I know that with python I can use sys.argv[x]. How do I do it with wiringPi (I'm new to wiringPi)
Cheers :)

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: sys.argv[1] with wiringPI

Sun Feb 28, 2016 10:31 am

I appreciate that English is probably not your first language, but it is not clear what you are asking about.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: sys.argv[1] with wiringPI

Sun Feb 28, 2016 10:33 am

Save the following as args.c and compile with gcc -o args args.c

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   int i, p1, p2;

   for (i=0; i<argc; i++) printf("arg%d is %s\n", i, argv[i]);

   if (argc>1) p1 = atoi(argv[1]); else p1=-1;
   if (argc>2) p2 = atoi(argv[2]); else p2=-2;

   printf("p1=%d p2=%d\n", p1, p2);

   return 0;
}
Run with ./args 23 45 dhhd sjsj

or any old random rubbish and you should get the idea.

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: sys.argv[1] with wiringPI

Sun Feb 28, 2016 10:44 am

joan wrote:Save the following as args.c and compile with gcc -o args args.c
.
A better answer is to learn about libpopt ! It should be one of the first libraries new C programmers are introduced to .

install libpopt-dev and read man 3 popt

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: sys.argv[1] with wiringPI

Sun Feb 28, 2016 10:53 am

PeterO wrote:
joan wrote:Save the following as args.c and compile with gcc -o args args.c
.
A better answer is to learn about libpopt ! It should be one of the first libraries new C programmers are introduced to .

install libpopt-dev and read man 3 popt

PeterO
A bit late for me I'm afraid. Depends on what you are doing - for throw away programs I'd do the above. For anything more serious I use getopt. This is the first I have heard of popt, Can't keep abreast of all these new fangled libraries.

User avatar
PeterO
Posts: 5880
Joined: Sun Jul 22, 2012 4:14 pm

Re: sys.argv[1] with wiringPI

Sun Feb 28, 2016 11:07 am

getopt is older but just as good for most cases. My point is that argument parsing is something that every program should do properly , even if all it does is print something meaningful in response to "-help" or "--help".

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

Re: sys.argv[1] with wiringPI

Sun Feb 28, 2016 11:46 am

Thanks joan!

Return to “General discussion”