Write a program that produces as hello message in the format Hello, <name>!
where <name>
is taken as a command line argument.
Example command line session 1
$ ./hello-cmd John
Hello, John!
Example command line session 2
$ ./hello-cmd Mary
Hello, Mary!
In Python,
you can access command line arguments
in the sys.argv
variable.
First import sys
then use
argv[0]
to see the program name
argv[1]
to see the first command line argument,
argv[2]
to see the second,
etc.
In C,
you can access command line arguments
by declaring main
with the following prototype:
int main(int argc, char *argv[])
Above,
argc
is the count of arguments
and argv
is an array of strings
where
argv[0]
is the program name,
argv[1]
is the first command line argument,
argv[2]
is the second and so on.
In Haskell,
you can access command line arguments
by using the function System.Environment.getArgs :: IO [String]
.
First import System.Environment
then
pull the result of getArgs
from the IO
monad.
The first element of the result will be the program name,
the second element will be the first command line argument,
the third element will be the second command line argument,
and so on.
try first: hi index-ints
try also: erro
try next: errxit
Copyright © 2020-2022 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/hello-cmd