summaryrefslogtreecommitdiff
path: root/bin/init_rust
diff options
context:
space:
mode:
Diffstat (limited to 'bin/init_rust')
-rwxr-xr-xbin/init_rust52
1 files changed, 41 insertions, 11 deletions
diff --git a/bin/init_rust b/bin/init_rust
index 1431b87..6425d63 100755
--- a/bin/init_rust
+++ b/bin/init_rust
@@ -1,25 +1,53 @@
#!/bin/sh -eu
-YEAR=${YEAR:-$(date +%Y)}
-DAY=${DAY:-$(TZ=EST date '+%d')}
+usage() {
+ print "%s\n\n%s\n%s\n%s\n" "$0 [-h] [-y <year>] [-d <day>]" \
+ '-h | --help Output this help.' \
+ '-y | --help Use another year than the current one.' \
+ '-d | --help Use another day than the current one.'
+}
+
+_opts=$( getopt --options "y:d:h" --long 'year:,day:,help' -- "$@" )
+
+eval set -- "${_opts}"
+unset _opts
-GIT_USER=$(git config --get 'user.name')
-GIT_EMAIL=$(git config --get 'user.email')
+while [ $1 != '--' ]; do
+ case "$1" in
+ '-d' | '--day')
+ shift
+ _day="$1"
+ ;;
+ '-y' | '--year')
+ shift
+ _year="$1"
+ ;;
+ '-h' | '--help')
+ usage
+ exit 0
+ ;;
+ esac
+ shift
+done
-CARGO=$( sed "s/^#\(.*${DAY}\)/\1/" <"${YEAR}/rust/Cargo.toml")
-echo "${CARGO}" >"${YEAR}/rust/Cargo.toml"
+_year=${_year:-$(date +%Y)}
+[ "${_day:-}" ] || _day=${_day:-$(TZ=EST date '+%d')}
-cd "${YEAR}/rust"
-mkdir "day${DAY}" || :
-cd "day${DAY}"
+[ "${#_day}" != 1 ] || _day="0${_day}"
+
+_cargo_contents=$( sed "s/^#\(.*${_day}\)/\1/" <"${_year}/rust/Cargo.toml")
+echo "${_cargo_contents}" >"${_year}/rust/Cargo.toml"
+
+cd "${_year}/rust"
+mkdir "day${_day}" || :
+cd "day${_day}"
cargo init --bin
cat > 'Cargo.toml' << END_OF_TOML
[package]
-name = "day${DAY}"
+name = "day${_day}"
version = "0.1.0"
-authors = ["${GIT_USER} <${GIT_EMAIL}>"]
edition = "2021"
[dependencies]
@@ -81,3 +109,5 @@ fn main() -> Result<()> {
Ok(())
}
END_OF_SRC
+
+unset _cargo_contents _day _year