writing beautiful command line programs
play

Writing Beautiful Command Line Programs John Leach @johnleach June - PowerPoint PPT Presentation

Writing Beautiful Command Line Programs John Leach @johnleach June 2012 John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 1 / 44 Intro Text only interfaces Text user interfaces (TUI) Command line interfaces (CLI)


  1. Writing Beautiful Command Line Programs John Leach @johnleach June 2012 John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 1 / 44

  2. Intro Text only interfaces Text user interfaces (TUI) Command line interfaces (CLI) John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 2 / 44

  3. Intro: Text User Interfaces Lotus 1-2-3 John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 3 / 44

  4. Intro: Text User Interfaces Microsoft Word 5.5 John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 4 / 44

  5. Intro: Text User Interfaces make menuconfig John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 5 / 44

  6. Intro: Command Line Interfaces shell $ uptime 12:59:52 up 16 days , 1:53 , 1 user , load average : 0.87 , 0.66 , 0.70 $ df − h F i l e s y s t e m S i z e Used A v a i l Use% Mounted on / dev / sda2 75G 64G 7.1G 90% / $ ping − c1 8 . 8 . 8 . 8 PING 8 . 8 . 8 . 8 ( 8 . 8 . 8 . 8 ) 56(84) bytes of data . 64 bytes from 8 . 8 . 8 . 8 : icmp_req=1 t t l =49 time=163 ms − 8 . 8 . 8 . 8 ping s t a t i s t i c s − − − − − 1 packets transmitted , 1 r e c e i v e d , 0% packet l o s s , time 0ms r t t min/avg/max/mdev = 163.747/163.747/163.747/0.000 ms John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 6 / 44

  7. Intro: Command Line Interfaces Brightbox Cloud API $ brightbox-servers list id status type zone created_on image_id cloud_ip_ids name ------------------------------------------------------------------------------------------ srv-f7dye active nano gb1-b 2010-12-03 img-9vxqi cip-8ks0v tor1 srv-v4rjq active mini gb1-b 2011-01-22 img-9vxqi cip-ye4ue ququ-web srv-uicfm active small gb1-a 2011-05-08 img-2ab98 cip-46qrn newsniffer srv-veehc active small gb1-a 2011-06-05 img-4gqhs cip-tqg43 web1 srv-crpty active nano gb1-a 2011-08-06 img-4gqhs cip-m1e98 humanweb srv-98rr6 active nano gb1-a 2011-11-25 img-3fsuy cip-8u19j claw srv-8hecw active mini gb1-a 2012-02-23 img-ytx00 cip-zd2me mycogen srv-aae27 inactive mini gb1-a 2012-04-11 img-715uq debiantest srv-qulcn active nano gb1-b 2012-05-09 img-9h5cv apacheds test srv-aa5jn active nano gb1-a 2012-05-09 img-9h5cv amq ------------------------------------------------------------------------------------------ John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 7 / 44

  8. Input and Output Input and Output stdin: text typed at a keyboard stdout: text emitted by the program stderr: more text emitted by the program John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 8 / 44

  9. Input and Output stdin, stdout, stderr in Ruby puts STDIN . i n s p e c t #<IO:0x7f353af81a08> puts STDOUT. i n s p e c t #<IO:0x7f0b08eb09f0> puts STDERR. i n s p e c t #<IO:0x7ff7e60df9b8> John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 9 / 44

  10. Input and Output stdin and stdout in Ruby echo " h e l l o " | ruby − e ’ puts STDIN . read ’ hello STDOUT. puts " world " world John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 10 / 44

  11. The ls command ls to your terminal $ l s . cmd i n i t . rb ruby − token . rb completion . rb input − method . rb s l e x . rb context . rb l c v e r s i o n . rb ext l o c a l e . rb workspace . rb extend − command . rb n o t i f i e r . rb ws − for − case − 2. rb frame . rb output − method . rb xmp . rb help . rb ruby − l e x . rb John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 11 / 44

  12. The ls command ls to another program $ l s . | grep ruby ruby − l e x . rb ruby − token . rb John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 12 / 44

  13. The ls command ls errors go where? $ l s . _why | grep ruby l s : cannot access _why : No such f i l e or d i r e c t o r y ruby − l e x . rb ruby − token . rb John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 13 / 44

  14. The ls command ls errors go to stderr $ l s . _why 2> e r r o r . log | grep ruby ruby − l e x . rb ruby − token . rb $ cat e r r o r . log l s : cannot access _why : No such f i l e or d i r e c t o r y John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 14 / 44

  15. The ls command ls for humans $ l s cmd i n i t . rb ruby − token . rb completion . rb input − method . rb s l e x . rb context . rb l c v e r s i o n . rb ext l o c a l e . rb workspace . rb extend − command . rb n o t i f i e r . rb ws − for − case − 2. rb frame . rb output − method . rb xmp . rb help . rb ruby − l e x . rb John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 15 / 44

  16. The ls command ls for programs $ l s . | grep ruby ruby − l e x . rb ruby − token . rb John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 16 / 44

  17. The ls command ls knows when you’re not human $ ls | cat cmd completion.rb context.rb ext extend-command.rb frame.rb help.rb init.rb input-method.rb lc locale.rb notifier.rb John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 17 / 44

  18. The ls command ls in for loops $ for f in ‘ ls ‘ ; do > echo Doing something with $f > done Doing something with cmd Doing something with completion . rb Doing something with context . rb Doing something with ext Doing something with extend − command . rb Doing something with frame . rb John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 18 / 44

  19. The ls command Detecting humans $ ruby − e ’ puts STDIN . i s a t t y . inspect ’ true $ echo h e l l o | ruby − e ’ puts STDIN . i s a t t y . inspect ’ f a l s e John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 19 / 44

  20. The terminal Terminal width 80 characters detect and adapt i r b ( main ):001:0 > ENV[ ’COLUMNS’ ] = > " 80 " i r b ( main ):002:0 > ENV[ ’ LINES ’ ] = > " 24 " $ s t t y s i z e 24 80 John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 20 / 44

  21. The terminal Use of colour ls grep only if a tty John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 21 / 44

  22. Tables Drawing nice tables with Hirb table [Time.now, Time.now + 3600], :fields => [:to_s, :to_i] +--------------------------------+------------+ | to_s | to_i | +--------------------------------+------------+ | Wed Jun 27 10:26:57 +0100 2012 | 1340789217 | | Wed Jun 27 11:26:57 +0100 2012 | 1340792817 | +--------------------------------+------------+ 2 rows in set John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 22 / 44

  23. Tables Vertical tables table [Time.now, Time.now + 3600], :fields => [:to_s, :to_i], :vertical => true ****** 1. row ****** to_s: Wed Jun 27 11:04:21 +0100 2012 to_i: 1340791461 ****** 2. row ****** to_s: Wed Jun 27 12:04:21 +0100 2012 to_i: 1340795061 2 rows in set John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 23 / 44

  24. Tables Hirb::Table vs. Brightbox::SimpleTable Brightbox::SimpleTable to_s to_i -------------------------------------------- Wed Jun 27 11:17:04 +0100 2012 1340792224 Wed Jun 27 12:17:04 +0100 2012 1340795824 -------------------------------------------- Hirb::Table +--------------------------------+------------+ | to_s | to_i | +--------------------------------+------------+ | Wed Jun 27 10:26:57 +0100 2012 | 1340789217 | | Wed Jun 27 11:26:57 +0100 2012 | 1340792817 | +--------------------------------+------------+ 2 rows in set John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 24 / 44

  25. Whitespace Whitespace and awk echo " h e l l o ␣␣ world " | awk ’{ p r i n t $2 } ’ world echo " h e l l o ␣ | ␣ world " | awk ’{ p r i n t $2 } ’ | echo " h e l l o ␣ | ␣ world " | awk − F ’ | ’ ’{ p r i n t $2 } ’ world John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 25 / 44

  26. Whitespace Awk and xargs in action $ brightbox-servers list | grep inactive | > awk ’{print $1}’ | xargs brightbox-servers show id: srv-aae27 status: inactive name: debiantest created_at: 2012-04-11T20:27Z deleted_at: zone: gb1-a type: typ-iqisj type_name: Brightbox Mini Instance type_handle: mini ram: 1024 cores: 4 disk: 40960 private_ips: 10.240.160.222 cloud_ips: ipv6_address: 2a02:1348:14c:2837:24:19ff:fef0:a0de cloud_ip_ids: hostname: srv-aae27 public_hostname: ipv6_hostname: ipv6.srv-aae27.gb1.brightbox.com snapshots: server_groups: grp-98v4n John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 26 / 44

  27. Whitespace Whitespace and the mouse If a mouse clicks and your program isn’t listening for it, does it do anything? John Leach (@johnleach) Writing Beautiful Command Line Programs June 2012 27 / 44

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend