Idiom #71 Echo program implementation
Basic implementation of the Echo program: Print all arguments except the program name, separated by space, followed by newline.
The idiom demonstrates how to skip the first argument if necessary, concatenate arguments as strings, append newline and print it to stdout.
- Ada
- C
- Clojure
- C++
- C#
- D
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- JS
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- Obj-C
- PHP
- Pascal
- Perl
- Prolog
- Python
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scala
- Smalltalk
int main(int argc, char *argv[])
{
while (*++argv) {
printf("%s", *argv);
if (argv[1]) printf(" ");
}
printf("\n");
return EXIT_SUCCESS;
}
void main(string[] args)
{
writeln(args.dropOne.joiner(" "));
}
.dropOne removes the first element of args, which usually is the program name.
.joiner does not allocate memory like .join, but loses features like random-access to the string and the result has to be written char-by-char to stdout.
.joiner does not allocate memory like .join, but loses features like random-access to the string and the result has to be written char-by-char to stdout.
main() ->
main(init:get_plain_arguments()).
main(ARGV) ->
io:format("~s~n", [lists:join(" ", ARGV)]).
main() or main/0 is not used if you run this with escript.
program x
implicit none
character (len=:), allocatable :: a
integer :: n, i, l
n = command_argument_count()
a = ''
do i=1,n
call get_command_argument(i, a, l)
if (l > len(a)) then
deallocate (a)
allocate (character(len=l) :: a)
call get_command_argument(i, a)
end if
write (unit=*,fmt='(A)', advance="no") a
if (i < n) then
write (unit=*,fmt='(" ")', advance="no")
else
write (unit=*,fmt='()')
end if
end do
end
a is a buffer which is allocated to the length needed.
func main() {
fmt.Println(strings.Join(os.Args[1:], " "))
}
console.log(process.argv.slice(2).join(" "));
In JavaScript, process.argv contains two entries that are to be skipped: The JavaScript interpreter, i.e. node, and the script name, i.e. echo.js.
public class Echo {
public static void main(final String... args) {
out.println(join(" ", args));
}
}
String.join() exists only since Java 8.
int main() {
NSArray *all=NSProcessInfo.processInfo.arguments;
NSArray *args=[all subarrayWithRange:NSMakeRange(1,all.count-1)];
puts([args componentsJoinedByString:@" "].UTF8String);
return 0;
}
The other solution is right (any plain-C code works in ObjC same way), but in practice, the object-oriented solution based on the Foundation library would be preferable
int main(int argc, char *argv[])
{
while (*++argv) {
printf("%s", *argv);
if (argv[1]) printf(" ");
}
printf("\n");
return EXIT_SUCCESS;
}
echo implode(' ', array_slice($argv, 1)), PHP_EOL;
Use PHP_EOL to be portable. This is ideal if you are printing this text to a terminal.
Alternatively, you can change PHP_EOL to just "\n" (UNIX Line Endings) or "\r\n" (Windows Line Endings) to specify a line ending.
Alternatively, you can change PHP_EOL to just "\n" (UNIX Line Endings) or "\r\n" (Windows Line Endings) to specify a line ending.
PROGRAM Echo;
VAR
I: Integer;
BEGIN
for I := 1 to ParamCount - 1 do
Write(ParamStr(I), ' ');
if (ParamCount > 0) then
Write(ParamStr(ParamCount));
WriteLn();
END.
main(Argv) :- echo(Argv).
echo([]) :- nl.
echo([Last]) :- write(Last), echo([]).
echo([H|T]) :- write(H), write(' '), echo(T).
| args arg sep |
args := CEnvironment commandLine readStream.
args next. " skip executable name "
sep := [args peek isNil not ifTrue: [' '] ifFalse: ['']].
[ (arg := args next) isNil ] whileFalse: [
Transcript show: arg; show: (sep value).
].
Transcript cr. " cr -> carriage return; ie. newline "
Needs to be run in headless mode so that Transcript prints to stdout.