Skip to content

Commit

Permalink
Merge pull request #529 from DannyBen/fix/default-command-usage
Browse files Browse the repository at this point in the history
Update default command usage to show optional brackets
  • Loading branch information
DannyBen authored Jul 5, 2024
2 parents 3bd01e5 + 854036d commit 5e9e322
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 87 deletions.
2 changes: 2 additions & 0 deletions examples/command-default/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ bashly generate
./ftp
./ftp -h
./ftp download something
./ftp upload
./ftp upload -h
./ftp upload something
./ftp something
7 changes: 6 additions & 1 deletion lib/bashly/script/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def summary_string

# Returns a constructed string suitable for Usage pattern
def usage_string
result = [full_name]
result = [base_usage_pattern]
command_string = default_command&.default == 'force' ? '[COMMAND]' : 'COMMAND'

result.push case mode
Expand All @@ -327,6 +327,11 @@ def usage_string_args
args.map(&:usage_string)
end

def base_usage_pattern
usage_pattern = default ? "[#{name}]" : name
parents.any? ? (parents + [usage_pattern]).join(' ') : usage_pattern
end

# Returns an array of files to include as is inside the script
# This is meant to provide the user with the ability to add custom
# functions
Expand Down
20 changes: 20 additions & 0 deletions spec/approvals/examples/command-default
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ Options:
# you can edit it freely and regenerate (it will not be overwritten)
args:
- ${args[source]} = something
+ ./ftp upload
missing required argument: SOURCE
usage: ftp [upload] SOURCE
+ ./ftp upload -h
ftp upload - Upload a file

Alias: u

Usage:
ftp [upload] SOURCE
ftp upload --help | -h

Options:
--help, -h
Show this help

Arguments:
SOURCE
File to upload

+ ./ftp upload something
# this file is located in 'src/upload_command.sh'
# code for 'ftp upload' goes here
Expand Down
2 changes: 1 addition & 1 deletion spec/approvals/examples/command-default-force
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ args: none
tester all - Run all tests

Usage:
tester all
tester [all]
tester all --help | -h

Options:
Expand Down
179 changes: 94 additions & 85 deletions spec/bashly/script/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,64 +405,73 @@
end
end

describe '#public_commands' do
let(:fixture) { :private_commands }
describe '#mode' do
context 'when flags and commands are defined' do
let(:fixture) { :mode_global_flags }

it 'returns an array of Command objects excluding private commands' do
expect(subject.public_commands.count).to eq 1
expect(subject.public_commands.first.name).to eq 'connect'
it 'returns :global_flags' do
expect(subject.mode).to eq :global_flags
end
end
end

describe '#public_commands_aliases' do
let(:fixture) { :private_commands }
context 'when only commands are defined' do
let(:fixture) { :mode_commands }

it 'returns an array of command aliases of public subcommands' do
expect(subject.public_command_aliases).to eq %w[connect c]
it 'returns :commands' do
expect(subject.mode).to eq :commands
end
end
end

describe '#user_file_path' do
it 'returns the path to the user file' do
expect(subject.user_file_path 'test.sh').to eq 'spec/tmp/src/test.sh'
end
context 'when args and flags are defined' do
let(:fixture) { :mode_args_and_flags }

context 'when the file argument does not end with .sh extension' do
it 'returns the path with .sh appended' do
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.sh'
it 'returns :args_and_flags' do
expect(subject.mode).to eq :args_and_flags
end
end

context 'when partials_extension is set and the argument does not end with the selected extension' do
before { Settings.partials_extension = 'bash' }
after { Settings.partials_extension = 'sh' }
context 'when only args are defined' do
let(:fixture) { :mode_args }

it 'returns the path with the selected extension appended' do
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.bash'
it 'returns :args' do
expect(subject.mode).to eq :args
end
end
end

describe '#user_file_exist?' do
before { FileUtils.mkdir_p 'spec/tmp/src' }

context 'when the file exists in the user source path' do
before { FileUtils.touch 'spec/tmp/src/test.sh' }
context 'when only flags are defined' do
let(:fixture) { :mode_flags }

it 'returns true' do
expect(subject.user_file_exist?('test')).to be true
it 'returns :flags' do
expect(subject.mode).to eq :flags
end
end

context 'when the file does not in the user source path' do
before { FileUtils.rm_f 'spec/tmp/src/test.sh' }
context 'when nothing is defined' do
let(:fixture) { :mode_empty }

it 'returns false' do
expect(subject.user_file_exist?('test')).to be false
it 'returns :empty' do
expect(subject.mode).to eq :empty
end
end
end

describe '#public_commands' do
let(:fixture) { :private_commands }

it 'returns an array of Command objects excluding private commands' do
expect(subject.public_commands.count).to eq 1
expect(subject.public_commands.first.name).to eq 'connect'
end
end

describe '#public_commands_aliases' do
let(:fixture) { :private_commands }

it 'returns an array of command aliases of public subcommands' do
expect(subject.public_command_aliases).to eq %w[connect c]
end
end

describe '#required_args' do
it 'returns an array of only the required Argument objects' do
expect(subject.required_args.size).to eq 1
Expand Down Expand Up @@ -546,56 +555,6 @@
end
end

describe '#mode' do
context 'when flags and commands are defined' do
let(:fixture) { :mode_global_flags }

it 'returns :global_flags' do
expect(subject.mode).to eq :global_flags
end
end

context 'when only commands are defined' do
let(:fixture) { :mode_commands }

it 'returns :commands' do
expect(subject.mode).to eq :commands
end
end

context 'when args and flags are defined' do
let(:fixture) { :mode_args_and_flags }

it 'returns :args_and_flags' do
expect(subject.mode).to eq :args_and_flags
end
end

context 'when only args are defined' do
let(:fixture) { :mode_args }

it 'returns :args' do
expect(subject.mode).to eq :args
end
end

context 'when only flags are defined' do
let(:fixture) { :mode_flags }

it 'returns :flags' do
expect(subject.mode).to eq :flags
end
end

context 'when nothing is defined' do
let(:fixture) { :mode_empty }

it 'returns :empty' do
expect(subject.mode).to eq :empty
end
end
end

describe '#usage_string' do
context 'when flags and commands are defined' do
let(:fixture) { :mode_global_flags }
Expand Down Expand Up @@ -660,6 +619,56 @@
end
end
end

context 'when the command is set as default' do
let(:fixture) { :default_command }

it 'returns the correct string' do
expect(subject.default_command.usage_string).to eq 'cli [get]'
end
end

end

describe '#user_file_path' do
it 'returns the path to the user file' do
expect(subject.user_file_path 'test.sh').to eq 'spec/tmp/src/test.sh'
end

context 'when the file argument does not end with .sh extension' do
it 'returns the path with .sh appended' do
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.sh'
end
end

context 'when partials_extension is set and the argument does not end with the selected extension' do
before { Settings.partials_extension = 'bash' }
after { Settings.partials_extension = 'sh' }

it 'returns the path with the selected extension appended' do
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.bash'
end
end
end

describe '#user_file_exist?' do
before { FileUtils.mkdir_p 'spec/tmp/src' }

context 'when the file exists in the user source path' do
before { FileUtils.touch 'spec/tmp/src/test.sh' }

it 'returns true' do
expect(subject.user_file_exist?('test')).to be true
end
end

context 'when the file does not in the user source path' do
before { FileUtils.rm_f 'spec/tmp/src/test.sh' }

it 'returns false' do
expect(subject.user_file_exist?('test')).to be false
end
end
end

describe '#whitelisted_args' do
Expand Down

0 comments on commit 5e9e322

Please sign in to comment.