Class: Benry::CmdApp::ApplicationHelpBuilder

Inherits:
BaseHelpBuilder show all
Defined in:
lib/benry/cmdapp.rb

Constant Summary

Constants inherited from BaseHelpBuilder

BaseHelpBuilder::HEADER_ABBREVS, BaseHelpBuilder::HEADER_ACTIONS, BaseHelpBuilder::HEADER_ALIASES, BaseHelpBuilder::HEADER_CATEGORIES, BaseHelpBuilder::HEADER_DESCRIPTION, BaseHelpBuilder::HEADER_OPTIONS, BaseHelpBuilder::HEADER_USAGE

Instance Method Summary collapse

Methods inherited from BaseHelpBuilder

#initialize

Constructor Details

This class inherits a constructor from Benry::CmdApp::BaseHelpBuilder

Instance Method Details

#build_help_message(gschema, all: false) ⇒ Object



1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
# File 'lib/benry/cmdapp.rb', line 1363

def build_help_message(gschema, all: false)
  #; [!ezcs4] returns help message string of application.
  #; [!ntj2y] includes hidden actions and options if `all: true` passed.
  sb = []
  sb << section_preamble()
  sb << section_usage()
  sb << section_description()
  sb << section_options(gschema, all: all)
  sb << section_actions(true, all: all)
  #sb << section_aliases(all: all)
  #sb << section_abbrevs(all: all)
  #sb << section_categories(0, all: all)
  sb << section_postamble()
  return sb.compact().join("\n")
end

#section_abbrevs(all: false) ⇒ Object



1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
# File 'lib/benry/cmdapp.rb', line 1539

def section_abbrevs(all: false)
  registry = @_registry
  format = @config.format_abbrev
  _ = all   # not used
  sb = []
  registry.abbrev_each do |abbrev, prefix|
    sb << format % [abbrev, prefix] << "\n"
  end
  #; [!dnt12] returns nil if no abbrevs found.
  return nil if sb.empty?
  #; [!00ice] returns abbrev list string.
  return render_section(header(:HEADER_ABBREVS), sb.join())  # "Abbreviations:"
end

#section_actions(include_aliases = true, all: false) ⇒ Object



1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
# File 'lib/benry/cmdapp.rb', line 1434

def section_actions(include_aliases=true, all: false)
  c = @config
  #; [!yn8ea] includes hidden actions into help message if `all: true` passed.
  str = (c.format_action, include_aliases, all: all) {|md|
    #; [!10qp0] includes aliases if the 1st argument is true.
    ! md.alias?
  }
  #; [!24by5] returns nil if no actions defined.
  return nil if str.empty?
  #; [!8qz6a] adds default action name after header if it is set.
  extra = c.default_action ? "(default: #{c.default_action})" : nil
  #; [!typ67] returns 'Actions:' section of help message.
  return render_section(header(:HEADER_ACTIONS), str, extra)  # "Actions:"
end

#section_aliases(all: false) ⇒ Object



1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
# File 'lib/benry/cmdapp.rb', line 1522

def section_aliases(all: false)
  registry = @_registry
  sb = []
  format = @config.format_action
  #; [!d7vee] ignores hidden aliases in default.
  #; [!4vvrs] include hidden aliases if `all: true` specifieid.
  #; [!v211d] sorts aliases by action names.
  registry.(all: all).select {|md| md.alias? }.sort_by {|md| [md.action, md.name] }.each do |md|
    s = format % [md.name, md.desc]
    sb << decorate_str(s, md.hidden?, md.important?) << "\n"
  end
  #; [!fj1c7] returns nil if no aliases found.
  return nil if sb.empty?
  #; [!496qq] renders alias list.
  return render_section(header(:HEADER_ALIASES), sb.join())  # "Aliases:"
end

#section_availables(include_aliases = true, all: false) ⇒ Object



1477
1478
1479
1480
1481
1482
# File 'lib/benry/cmdapp.rb', line 1477

def section_availables(include_aliases=true, all: false)
  #; [!pz0cu] includes 'Actions:' and 'Aliases:' sections.
  s1 = section_actions(include_aliases, all: all)
  s2 = section_aliases(all: all)
  return [s1, s2].compact.join("\n")
end

#section_candidates(prefix, all: false) ⇒ Object



1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
# File 'lib/benry/cmdapp.rb', line 1484

def section_candidates(prefix, all: false)
  c = @config
  registry = @_registry
  #; [!idm2h] includes hidden actions when `all: true` passed.
  prefix2 = prefix.chomp(':')
  str = (c.format_action, all: all) {||
    #; [!duhyd] includes actions which name is same as prefix.
    #; [!nwwrd] if prefix is 'xxx:' and alias name is 'xxx' and action name of alias matches to 'xxx:', skip it because it will be shown in 'Aliases:' section.
    _category_action?(, prefix)
  }
  #s1 = str.empty? ? nil : render_section(header(:HEADER_ACTIONS), str)
  s1 = render_section(header(:HEADER_ACTIONS), str)
  #; [!otvbt] includes name of alias which corresponds to action starting with prefix.
  #; [!h5ek7] includes hidden aliases when `all: true` passed.
  str = (c.format_action, all: all) {||
    .alias? && .action.start_with?(prefix)
  }
  #; [!9lnn2] alias names in candidate list are sorted by action name.
  str = str.each_line.sort_by {|line|
    line =~ /'([^']+)'/
    [$1, line]
  }.join()
  #; [!80t51] alias names are displayed in separated section from actions.
  s2 = str.empty? ? nil : render_section(header(:HEADER_ALIASES), str)
  #; [!rqx7w] returns header string if both no actions nor aliases found with names starting with prefix.
  #; [!3c3f1] returns list of actions which name starts with prefix specified.
  return [s1, s2].compact().join("\n")
end

#section_categories(depth = 0, all: false) ⇒ Object



1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
# File 'lib/benry/cmdapp.rb', line 1553

def section_categories(depth=0, all: false)
  registry = @_registry
  c = @config
  #; [!30l2j] includes number of actions per prefix.
  #; [!alteh] includes prefix of hidden actions if `all: true` passed.
  dict = registry.category_count_actions(depth, all: all)
  #registry.category_each {|prefix, _| dict[prefix] = 0 unless dict.key?(prefix) }
  #; [!p4j1o] returns nil if no prefix found.
  return nil if dict.empty?
  #; [!k3y6q] uses `config.format_category` or `config.format_action`.
  format = (c.format_category || c.format_action) + "\n"
  indent = /^( *)/.match(format)[1]
  str = dict.keys.sort.collect {|prefix|
    s = "#{prefix} (#{dict[prefix]})"
    #; [!qxoja] includes category description if registered.
    desc = registry.category_get_desc(prefix)
    desc ? (format % [s, desc]) : "#{indent}#{s}\n"
  }.join()
  #; [!crbav] returns top prefix list.
  return render_section(header(:HEADER_CATEGORIES), str, "(depth=#{depth})")  # "Categories:"
end