Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TypeScriptのコマンドラインパーサー

 TypeScriptのコマンドラインパーサー

9a3693c0c3d12f2fbae51792dd6a1344?s=128

Akito0107

June 27, 2019
Tweet

Transcript

  1. TypeScriptͰ ίϚϯυϥΠϯ
 ύʔαʔ NodeֶԂ34ݶ໨ ҏ౻ɹӯ

  2. ࣗݾ঺հ • ҏ౻ɹӯ (͍ͱ͏ɹ͖͋ͱʣ • @akito0107 • όοΫΤϯυΤϯδχΞ • Go

    / TypeScriptΛॻ͍͍ͯ·͢ • https://blog.akito0107.dev
  3. ໨࣍ • ίϚϯυϥΠϯύʔαʔΛ࡞ͬͨ࿩ • ͳͥ࡞͔ͬͨ • Demo • ࢖͍ํʹ͍ͭͯ

  4. Node.jsͰ
 ίϚϯυϥΠϯπʔϧΛ࡞Δ • ҎԼͷΑ͏ͳϥΠϒϥϦ͕༗໊ • substack/minimist • tj/commander • cacjs/cac

    • yargs/yargs • ෳࡶͳΦϓγϣϯΛύʔε͢Δͱ͖ʹਏ͍
  5. Option Hell • ex) kubectl


  6. Sub command΋ ͭΒ͍

  7. ίϚϯυϥΠϯͷΦϓγϣϯ • ͋Δఔ౓ෳࡶͳίϚϯυΛ࣮૷͠Α͏ͱͨ͠ ৔߹ɺΦϓγϣϯͷ਺͕Ͳ͏ͯ͠΋๲Ε্ ͕ͬͯ͠·͏ • طଘϥΠϒϥϦͰෳࡶͳΦϓγϣϯΛѻ͓͏ ͱ͢Δͱݫ͍͠(ͱࢥͬͯΔ)

  8. ex) commander

  9. ex) commander ܕ͕΄͍͠ʂʂʂʂ

  10. ܕ͕ͭ͘΍ͭΛ࡞ͬͨ • https://github.com/akito0107/marron-glace • σϞ

  11. ϑϥάͷఆٛͷ࢓ํ • makeStringFlag / makeBooleanFlag … • ͜ΕΒͷؔ਺ʹɺϑϥάͷ໊લͱΦϓγϣϯ ʢσϑΥϧτ࣌ͷ஋ɺhelpͰදࣔ͢Δจݴͳ ͲʣΛ౉͢

  12. <= Stringͷͱ͖͸stringҎ֎ڐ༰͠ͳ͍ <=helpͷͱ͖ͷจݴ <= -s Ͱ΋ड͚ΒΕΔΑ͏ʹ —flag1 ͷϑϥάΛఆٛ —flag2 ͷϑϥάΛఆٛ

  13. ϑϥάͷreduce • reduceFlagͰෳ਺ͷϑϥάΛ·ͱΊΔ • reduceFlagͷฦΓ஋΋flagͳͷͰɺ࠶౓ reduce͢Δ͜ͱ͕Ͱ͖Δ

  14. makeCommand • flagΛmakeCommandͷύϥϝʔλʹ౉͢ <= ͜ͷ͋ͨΓ͸helpͰग़͢΍ͭ

  15. (positional)Arguments • `mv src target` ͷsrc, targetͷΑ͏ʹɺίϚϯ υʹΘͨ͢Ҿ਺΋ܕ෇͖Ͱαϙʔτ

  16. makeCommandʹΘͨ͢ <= ͜͜ʹิ׬͕ޮ͘

  17. Sub commands • makeSubcommandHandlerΛ࢖͏ <= ͜͜ʹsubCommandΛ͍ΕΔ

  18. Sub commandΛ࡞Δ <= ਌ίϚϯυͷflag <= subίϚϯυͷflag ↑ ਌ίϚϯυͱreduce͢Δ <- ਌ίϚϯυͷflagͱ


    subίϚϯυͷflag͕྆ํ࢖͑Δ
  19. ͦͷଞ • —helpͰϔϧϓίϚϯυ • ಠࣗܕͷflag • ৄ͘͠͸ϒϩάʹ

  20. ·ͱΊ • TypeScriptܕਪ࿦͕όϦόϦޮ͘ίϚϯυϥ ΠϯπʔϧΛ࡞ͬͨ • όάͳͲ͋Γ·ͨ͠Β࿈བྷ͍ͯͩ͘͠͞ʂ

  21. ͋Γ͕ͱ͏͍͟͝·ͨ͠