summaryrefslogtreecommitdiff
path: root/chapter25/docs
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-08-12 11:20:14 -0700
committerCliff Click <cliffc@acm.org>2026-08-12 11:20:14 -0700
commit1cb8279a64f4f79615ac8358927b11237df29bf2 (patch)
treed057b4ee723a9d29e16f2c71959c0ae2773cdb63 /chapter25/docs
parent628452bb9f6137d433395fae5a9f5d780fb0f1e1 (diff)
downloadSimple-1cb8279a64f4f79615ac8358927b11237df29bf2.tar.gz
Simple-1cb8279a64f4f79615ac8358927b11237df29bf2.zip
Real argc/argv args
Beef up BubbleSort error handling
Diffstat (limited to 'chapter25/docs')
-rw-r--r--chapter25/docs/examples/BubbleSort.smp22
1 files changed, 14 insertions, 8 deletions
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;
};