From 1cb8279a64f4f79615ac8358927b11237df29bf2 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Wed, 12 Aug 2026 11:20:14 -0700 Subject: Real argc/argv args Beef up BubbleSort error handling --- chapter25/docs/examples/BubbleSort.smp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'chapter25/docs/examples') diff --git a/chapter25/docs/examples/BubbleSort.smp b/chapter25/docs/examples/BubbleSort.smp index 864e26da..1a4359ae 100644 --- a/chapter25/docs/examples/BubbleSort.smp +++ b/chapter25/docs/examples/BubbleSort.smp @@ -17,17 +17,23 @@ val sort = { sys.aryi64 nums -> } }; -// Read from stdin a line of the form "[ 4, 3, 2, 1]" and print it sorted. -val main = { -> - // Parse array of numbers from stdin - var nums = sys.aryi64.parseAryI64( sys.io.stdin() ); - if( !nums ) - return sys.io.p("stdin must look like [4, 3, 2, 1]"); +// Read one process argument of the form "[ 4, 3, 2, 1]" and print it sorted. +// args[0] is the executable name, as with native C argc/argv. +val main = { u8[~][] args -> + val usage = { -> + sys.io.p("Usage: please provide a list of at least two integers to sort in the format \"[1, 2, 3, 4, 5]\"\n"); + return 0; + }; + if( args# != 2 ) return usage(); + + var nums = sys.aryi64.parseAryI64(args[1]); + if( !nums ) return usage(); + if( nums.len < 2 ) return usage(); // Bubble sort array of integers sort(nums); - // Print a list of numbers to stdout - nums.str().write(1); + // Print the comma-separated result without the input brackets. + nums.csv().write(1); return 0; }; -- cgit v1.2.3