Skip to content

Commit 5e9e322

Browse files
authored
Merge pull request #529 from DannyBen/fix/default-command-usage
Update default command usage to show optional brackets
2 parents 3bd01e5 + 854036d commit 5e9e322

File tree

5 files changed

+123
-87
lines changed

5 files changed

+123
-87
lines changed

examples/command-default/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ bashly generate
1111
./ftp
1212
./ftp -h
1313
./ftp download something
14+
./ftp upload
15+
./ftp upload -h
1416
./ftp upload something
1517
./ftp something

lib/bashly/script/command.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def summary_string
307307

308308
# Returns a constructed string suitable for Usage pattern
309309
def usage_string
310-
result = [full_name]
310+
result = [base_usage_pattern]
311311
command_string = default_command&.default == 'force' ? '[COMMAND]' : 'COMMAND'
312312

313313
result.push case mode
@@ -327,6 +327,11 @@ def usage_string_args
327327
args.map(&:usage_string)
328328
end
329329

330+
def base_usage_pattern
331+
usage_pattern = default ? "[#{name}]" : name
332+
parents.any? ? (parents + [usage_pattern]).join(' ') : usage_pattern
333+
end
334+
330335
# Returns an array of files to include as is inside the script
331336
# This is meant to provide the user with the ability to add custom
332337
# functions

spec/approvals/examples/command-default

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ Options:
4141
# you can edit it freely and regenerate (it will not be overwritten)
4242
args:
4343
- ${args[source]} = something
44+
+ ./ftp upload
45+
missing required argument: SOURCE
46+
usage: ftp [upload] SOURCE
47+
+ ./ftp upload -h
48+
ftp upload - Upload a file
49+
50+
Alias: u
51+
52+
Usage:
53+
ftp [upload] SOURCE
54+
ftp upload --help | -h
55+
56+
Options:
57+
--help, -h
58+
Show this help
59+
60+
Arguments:
61+
SOURCE
62+
File to upload
63+
4464
+ ./ftp upload something
4565
# this file is located in 'src/upload_command.sh'
4666
# code for 'ftp upload' goes here

spec/approvals/examples/command-default-force

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ args: none
3737
tester all - Run all tests
3838

3939
Usage:
40-
tester all
40+
tester [all]
4141
tester all --help | -h
4242

4343
Options:

spec/bashly/script/command_spec.rb

Lines changed: 94 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -405,64 +405,73 @@
405405
end
406406
end
407407

408-
describe '#public_commands' do
409-
let(:fixture) { :private_commands }
408+
describe '#mode' do
409+
context 'when flags and commands are defined' do
410+
let(:fixture) { :mode_global_flags }
410411

411-
it 'returns an array of Command objects excluding private commands' do
412-
expect(subject.public_commands.count).to eq 1
413-
expect(subject.public_commands.first.name).to eq 'connect'
412+
it 'returns :global_flags' do
413+
expect(subject.mode).to eq :global_flags
414+
end
414415
end
415-
end
416416

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

420-
it 'returns an array of command aliases of public subcommands' do
421-
expect(subject.public_command_aliases).to eq %w[connect c]
420+
it 'returns :commands' do
421+
expect(subject.mode).to eq :commands
422+
end
422423
end
423-
end
424424

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

430-
context 'when the file argument does not end with .sh extension' do
431-
it 'returns the path with .sh appended' do
432-
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.sh'
428+
it 'returns :args_and_flags' do
429+
expect(subject.mode).to eq :args_and_flags
433430
end
434431
end
435432

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

440-
it 'returns the path with the selected extension appended' do
441-
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.bash'
436+
it 'returns :args' do
437+
expect(subject.mode).to eq :args
442438
end
443439
end
444-
end
445440

446-
describe '#user_file_exist?' do
447-
before { FileUtils.mkdir_p 'spec/tmp/src' }
448-
449-
context 'when the file exists in the user source path' do
450-
before { FileUtils.touch 'spec/tmp/src/test.sh' }
441+
context 'when only flags are defined' do
442+
let(:fixture) { :mode_flags }
451443

452-
it 'returns true' do
453-
expect(subject.user_file_exist?('test')).to be true
444+
it 'returns :flags' do
445+
expect(subject.mode).to eq :flags
454446
end
455447
end
456448

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

460-
it 'returns false' do
461-
expect(subject.user_file_exist?('test')).to be false
452+
it 'returns :empty' do
453+
expect(subject.mode).to eq :empty
462454
end
463455
end
464456
end
465457

458+
describe '#public_commands' do
459+
let(:fixture) { :private_commands }
460+
461+
it 'returns an array of Command objects excluding private commands' do
462+
expect(subject.public_commands.count).to eq 1
463+
expect(subject.public_commands.first.name).to eq 'connect'
464+
end
465+
end
466+
467+
describe '#public_commands_aliases' do
468+
let(:fixture) { :private_commands }
469+
470+
it 'returns an array of command aliases of public subcommands' do
471+
expect(subject.public_command_aliases).to eq %w[connect c]
472+
end
473+
end
474+
466475
describe '#required_args' do
467476
it 'returns an array of only the required Argument objects' do
468477
expect(subject.required_args.size).to eq 1
@@ -546,56 +555,6 @@
546555
end
547556
end
548557

549-
describe '#mode' do
550-
context 'when flags and commands are defined' do
551-
let(:fixture) { :mode_global_flags }
552-
553-
it 'returns :global_flags' do
554-
expect(subject.mode).to eq :global_flags
555-
end
556-
end
557-
558-
context 'when only commands are defined' do
559-
let(:fixture) { :mode_commands }
560-
561-
it 'returns :commands' do
562-
expect(subject.mode).to eq :commands
563-
end
564-
end
565-
566-
context 'when args and flags are defined' do
567-
let(:fixture) { :mode_args_and_flags }
568-
569-
it 'returns :args_and_flags' do
570-
expect(subject.mode).to eq :args_and_flags
571-
end
572-
end
573-
574-
context 'when only args are defined' do
575-
let(:fixture) { :mode_args }
576-
577-
it 'returns :args' do
578-
expect(subject.mode).to eq :args
579-
end
580-
end
581-
582-
context 'when only flags are defined' do
583-
let(:fixture) { :mode_flags }
584-
585-
it 'returns :flags' do
586-
expect(subject.mode).to eq :flags
587-
end
588-
end
589-
590-
context 'when nothing is defined' do
591-
let(:fixture) { :mode_empty }
592-
593-
it 'returns :empty' do
594-
expect(subject.mode).to eq :empty
595-
end
596-
end
597-
end
598-
599558
describe '#usage_string' do
600559
context 'when flags and commands are defined' do
601560
let(:fixture) { :mode_global_flags }
@@ -660,6 +619,56 @@
660619
end
661620
end
662621
end
622+
623+
context 'when the command is set as default' do
624+
let(:fixture) { :default_command }
625+
626+
it 'returns the correct string' do
627+
expect(subject.default_command.usage_string).to eq 'cli [get]'
628+
end
629+
end
630+
631+
end
632+
633+
describe '#user_file_path' do
634+
it 'returns the path to the user file' do
635+
expect(subject.user_file_path 'test.sh').to eq 'spec/tmp/src/test.sh'
636+
end
637+
638+
context 'when the file argument does not end with .sh extension' do
639+
it 'returns the path with .sh appended' do
640+
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.sh'
641+
end
642+
end
643+
644+
context 'when partials_extension is set and the argument does not end with the selected extension' do
645+
before { Settings.partials_extension = 'bash' }
646+
after { Settings.partials_extension = 'sh' }
647+
648+
it 'returns the path with the selected extension appended' do
649+
expect(subject.user_file_path 'test').to eq 'spec/tmp/src/test.bash'
650+
end
651+
end
652+
end
653+
654+
describe '#user_file_exist?' do
655+
before { FileUtils.mkdir_p 'spec/tmp/src' }
656+
657+
context 'when the file exists in the user source path' do
658+
before { FileUtils.touch 'spec/tmp/src/test.sh' }
659+
660+
it 'returns true' do
661+
expect(subject.user_file_exist?('test')).to be true
662+
end
663+
end
664+
665+
context 'when the file does not in the user source path' do
666+
before { FileUtils.rm_f 'spec/tmp/src/test.sh' }
667+
668+
it 'returns false' do
669+
expect(subject.user_file_exist?('test')).to be false
670+
end
671+
end
663672
end
664673

665674
describe '#whitelisted_args' do

0 commit comments

Comments
 (0)