# Writing Your Scripts

The bashly generate command is performing the following actions:

  1. Generates placeholder files in the src directory - one file for each of the defined commands in your bashly.yml file. These files are generated only once and are never overwritten.
  2. Merges all these partial scripts into a single bash script, and saves it in the root directory of your project.

# Processing user input

In order to access the parsed arguments in any of your partial scripts, you may simply access the $args associative array.

For example:

# Generate a minimal configuration:
$ bashly init --minimal

# Generate the bash script:
$ bashly generate

# Run the script:
$ ./download hello --force
# this file is located in 'src/root_command.sh'
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[--force]} = 1
- ${args[source]} = hello

You will notice that all the arguments of the associative array are printed on screen. This is done by the inspect_args function that was inserted into the generated partial script src/root_command.sh.

You can now access these variables by modifying src/root_command.sh like this:

src/root_command.sh
source_url=${args[source]}
force=${args[--force]}

if [[ $force ]]; then
  echo "downloading $source_url with --force"
else
  echo "downloading $source_url"
fi

After editing the file, run these commands:

# Regenerate the script:
$ bashly generate   # or bashly g for short

# Test its new functionality:
$ ./download a --force
downloading a with --force

# Adding common functions

In case you wish to add functions that can be used from multiple locations in your code, you can place *.sh files inside the src/lib - these files will be merged as is to the final bash script.

To get a starting point, you can run the convenience command:

$ bashly add lib

Custom Includes Example

# Hooks

# Initialization

Any code within the src/initialize.sh file will be called before anything else in your generated bash script.

# Before/after hooks

Any code within the src/before.sh file will be called before executing any command, but after processing and validating the command line. Similarly, any code within the src/after.sh file will be called after executing any command.

Function Hooks
../../advanced/hooks/

# Custom header

In case you wish to replace the header in the generated script, simply put the new content in src/header.sh.

# Hidden comments

Any comment in your source files that begins with two # symbols, will be removed from the final generated script. This is ideal for adding developer notes that should not be visible to your end users.

## this comment will be hidden
# this one will be visible